Bloodshot

Fibonacci Calculator

Jun 11th, 2012
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 4.29 KB | None | 0 0
  1. package fibonacci;
  2.  
  3. import java.awt.Toolkit;
  4. import java.awt.datatransfer.Clipboard;
  5. import java.awt.datatransfer.StringSelection;
  6. import java.math.BigInteger;
  7. import java.util.Scanner;
  8.  
  9. public class Main {
  10.  
  11.     public static void main(String[] args) {
  12.         //String s1 = "656006096262142989350178033729549556642676383232336265433784409742154433804661332137484567136571199328212724757371571537299468910158925926520634267410117568569929788383355349652486489818108622969402477887141685962903374297020809706805196713227956014251877900218898169451536868965760698964216982448169886501238156520802621307753044482574180109648240541866529324983414010189670221762155494932626210505";
  13.         //String s2 = "250572031945003272238062679321828252833825213808823514984330268366978962197341129230519532039014797883605808330899720778605308114380839942796889389428927605299078556674667262716674388794375852673649320630692741493085455843731600778404958287462585565120043104898780253688088872688246379722105843535447234778633004665956274642418148252674366749520021561386892682686265905694179065413231025238392035696";
  14.         //System.out.println(skipStep(s1, s2));
  15.         //System.out.println(addBig(s1, s2));
  16.         Scanner sc = new Scanner(System.in);
  17.         int term = 0;
  18.         while (true) {
  19.             String s = sc.nextLine().trim().toLowerCase();
  20.             boolean form, bare = false;
  21.             if (s.startsWith("f ")) {
  22.                 form = true;
  23.                 s = s.substring(s.indexOf(" ") + 1);
  24.             } else if (s.startsWith("b ")) {
  25.                 bare = true;
  26.                 s = s.substring(s.indexOf(" ") + 1);
  27.             }
  28.             if (s.equals("") || s.equals("n")) {
  29.                 term++;
  30.             } else if (s.equals("s")) {
  31.                 term += 2;
  32.             } else if (s.startsWith("c ")) {
  33.                 String[] parts = s.split(" ");
  34.                 try {
  35.                     clip(addBig(parts[1], parts[2]).toString());
  36.                 } catch (ArrayIndexOutOfBoundsException e) {
  37.                     System.out.println("You must put two numbers to add!");
  38.                 }
  39.                 continue;
  40.             } else if (s.equals("help")) {
  41.                 System.out.println("Number: Clip the nth term.");
  42.                 System.out.println("'n' or nothing: Calculate the next term.");
  43.                 System.out.println("'s': calculate the one after the next term.");
  44.                 System.out.println("'c [NUMBER] [NUMBER]': add two big numbers.");
  45.                 System.out.println("'b [NUMBER': calculate the nth term, but do not format it. Can preceed 's' or 'n'");
  46.                 continue;
  47.             } else {
  48.                 try {
  49.                     term = Integer.parseInt(s);
  50.                 } catch (NumberFormatException e) {
  51.                     System.out.println("Invalid input! Enter 'help' for valid inputs!");
  52.                 }
  53.             }
  54.             if (bare) {
  55.                 clip(calc(term).toString());
  56.             } else {
  57.                 clip(calcLine(term));
  58.             }
  59.         }
  60.     }
  61.     private static Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
  62.  
  63.     private static void clip(String s) {
  64.         StringSelection ss = new StringSelection(s);
  65.         c.setContents(ss, null);
  66.     }
  67.  
  68.     public static BigInteger addBig(String number1, String number2) {
  69.         BigInteger num1 = new BigInteger(number1), num2 = new BigInteger(number2);
  70.         return num1.add(num2);
  71.     }
  72.  
  73.     public static BigInteger skipStep(String number1, String number2) {
  74.         BigInteger num1 = new BigInteger(number1), num2 = new BigInteger(number2);
  75.         BigInteger firstStep = num1.add(num2);
  76.         return firstStep.add(num1.max(num2)); //This skips a step in the Fibonacci sequence, by adding the bigger number twice.
  77.     }
  78.  
  79.     public static BigInteger calc(int num) {
  80.         BigInteger num1 = new BigInteger("1"), num2 = new BigInteger("2");
  81.         for (int x = 3; x < num; x++) {
  82.             num1 = num1.add(num2);
  83.             x++;
  84.             if (x == num) {
  85.                 return num1;
  86.             }
  87.             num2 = num2.add(num1);
  88.         }
  89.         return num2;
  90.     } //1 1 2 3 5 8 13 21 34
  91.  
  92.     public static String calcLine(int num) {
  93.         return "F(" + num + ") =\n\n" + calc(num);
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment