Kaidul

Fibonacci Freeze

Dec 25th, 2012
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.88 KB | None | 0 0
  1. import java.math.BigInteger;
  2. import java.util.Scanner;
  3.  
  4.  
  5. class Main {
  6.    
  7.     static final int Max = 5000;
  8.     static BigInteger[] dp = new BigInteger[Max+10];
  9.    
  10.     static BigInteger fiboNacci(int n) {
  11.         if(n == 0) {
  12.             dp[n] = BigInteger.ZERO;
  13.             return dp[n];
  14.         }
  15.         if (n == 1) {
  16.             dp[n] = BigInteger.ONE;
  17.             return dp[n];
  18.         }
  19.         if(dp[n].equals(new BigInteger("-1"))) {
  20.             dp[n] = fiboNacci(n-1).add(fiboNacci(n-2));
  21.             return dp[n];          
  22.         } else {
  23.             return dp[n];
  24.         }
  25.     }
  26.  
  27.     public static void main(String[] args) {
  28.        
  29.         BigInteger minusOne = new BigInteger("-1");
  30.         for (int i = 0; i <= Max; i++) {
  31.             dp[i] = minusOne;
  32.         }
  33.         fiboNacci(Max);
  34.         Scanner input = new Scanner(System.in);
  35.        
  36.         while (input.hasNextInt()) {
  37.             int n = input.nextInt();
  38.             System.out.println("The Fibonacci number for " + n + " is " + dp[n]);
  39.         }
  40.     }
  41.  
  42. }
Add Comment
Please, Sign In to add comment