Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.math.BigInteger;
- import java.util.Scanner;
- class Main {
- static final int Max = 5000;
- static BigInteger[] dp = new BigInteger[Max+10];
- static BigInteger fiboNacci(int n) {
- if(n == 0) {
- dp[n] = BigInteger.ZERO;
- return dp[n];
- }
- if (n == 1) {
- dp[n] = BigInteger.ONE;
- return dp[n];
- }
- if(dp[n].equals(new BigInteger("-1"))) {
- dp[n] = fiboNacci(n-1).add(fiboNacci(n-2));
- return dp[n];
- } else {
- return dp[n];
- }
- }
- public static void main(String[] args) {
- BigInteger minusOne = new BigInteger("-1");
- for (int i = 0; i <= Max; i++) {
- dp[i] = minusOne;
- }
- fiboNacci(Max);
- Scanner input = new Scanner(System.in);
- while (input.hasNextInt()) {
- int n = input.nextInt();
- System.out.println("The Fibonacci number for " + n + " is " + dp[n]);
- }
- }
- }
Add Comment
Please, Sign In to add comment