Guest User

Untitled

a guest
Apr 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.72 KB | None | 0 0
  1. import java.math.BigInteger;
  2. import static java.math.BigInteger.ZERO;
  3. import static java.math.BigInteger.ONE;
  4.  
  5. public class Fibonacci {
  6.  
  7.     private static final BigInteger TWO = new BigInteger("2");
  8.    
  9.     public static BigInteger fib(BigInteger n) {
  10.         if(n.equals(ZERO))
  11.             return ZERO;
  12.         else if(n.equals(ONE))
  13.             return ONE;
  14.         else
  15.             return fib(n.subtract(ONE)).add(fib(n.subtract(TWO)));
  16.     }
  17.    
  18.     private static BigInteger fib(int n, int i, BigInteger f1, BigInteger f2) {
  19.         if(i == n)
  20.             return f1.add(f2);
  21.         return null;
  22.     }
  23.    
  24.     public static BigInteger fib(int n) {
  25.         return fib(n, 0, ZERO, ONE);
  26.     }
  27.    
  28.     public static void main(String[] args) {
  29.         System.out.println(fib(new BigInteger("20000000")));
  30.     }
  31.  
  32. }
Add Comment
Please, Sign In to add comment