Advertisement
Bloodshot

Fibonacci

Jun 10th, 2012
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.38 KB | None | 0 0
  1. import java.awt.Toolkit;
  2. import java.awt.datatransfer.StringSelection;
  3. import java.math.BigInteger;
  4. import java.util.Scanner;
  5.  
  6. public class Main {
  7.  
  8.     public static void main(String[] args) {
  9.         //String s1 = "656006096262142989350178033729549556642676383232336265433784409742154433804661332137484567136571199328212724757371571537299468910158925926520634267410117568569929788383355349652486489818108622969402477887141685962903374297020809706805196713227956014251877900218898169451536868965760698964216982448169886501238156520802621307753044482574180109648240541866529324983414010189670221762155494932626210505";
  10.         //String s2 = "250572031945003272238062679321828252833825213808823514984330268366978962197341129230519532039014797883605808330899720778605308114380839942796889389428927605299078556674667262716674388794375852673649320630692741493085455843731600778404958287462585565120043104898780253688088872688246379722105843535447234778633004665956274642418148252674366749520021561386892682686265905694179065413231025238392035696";
  11.         //System.out.println(skipStep(s1, s2));
  12.         //System.out.println(addBig(s1, s2));
  13.         Scanner sc = new Scanner(System.in);
  14.         while(true){
  15.             StringSelection ss = new StringSelection(calcFormatted(Integer.parseInt(sc.nextLine())));
  16.             Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
  17.         }
  18.     }
  19.    
  20.     public static BigInteger addBig(String number1, String number2){
  21.         BigInteger num1 = new BigInteger(number1), num2 = new BigInteger(number2);
  22.         return num1.add(num2);
  23.     }
  24.    
  25.     public static BigInteger skipStep(String number1, String number2){
  26.         BigInteger num1 = new BigInteger(number1), num2 = new BigInteger(number2);
  27.         BigInteger firstStep = num1.add(num2);
  28.         return firstStep.add(num1.max(num2)); //This skips a step in the Fibonacci sequence, by adding the bigger number twice.
  29.     }
  30.    
  31.     public static BigInteger calc(int num){
  32.         BigInteger num1 = new BigInteger("1"), num2 = new BigInteger("2");
  33.         for (int x = 3; x < num; x++){
  34.             num1 = num1.add(num2);
  35.             x++;
  36.             if (x == num){
  37.                 return num1;
  38.             }
  39.             num2 = num2.add(num1);
  40.         }
  41.         return num2;
  42.     } //1 1 2 3 5 8 13 21 34
  43.    
  44.     public static String calcFormatted(int num){
  45.         return "F(" + num + ") =\n\n" + calc(num);
  46.     }
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement