import java.math.*; import java.io.*; public class FibonacciTest { private static BigInteger[] answers; private static BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) ); private static BigInteger one; private static BigInteger zero; public static int fibonacci(int n) { if((n == 1) || (n == 2)) return 1; else return fibonacci(n-1) + fibonacci(n-2); } public static BigInteger fastFibonacci(int n) { if((n == 1) || (n == 2)) return answers[0]; if(answers[n-1].compareTo(zero) != 0) return answers[n-1]; if(answers[n-2].compareTo(zero) == 0) answers[n-2] = fastFibonacci(n-1); if(answers[n-3].compareTo(zero) == 0) answers[n-3] = fastFibonacci(n-2); return answers[n-2].add(answers[n-3]); } public static void main(String[] args) { int n; // Prompt the user System.out.println("Type a positive integer." ); try{ // Read a line of text from the user. String input = stdin.readLine(); // converts a String into an int value n = Integer.parseInt( input ); zero = new BigInteger("0"); one = new BigInteger("1"); // Initializing answers answers = new BigInteger[n]; answers[0] = new BigInteger("1"); answers[1] = new BigInteger("1"); for(int i = 2; i < n; i++) answers[i] = new BigInteger("0"); System.out.println("The "+n+"th Fibonacci number is "+fastFibonacci(n)); //System.out.println("The "+n+"th Fibonacci number is "+fibonacci(n)); } catch(java.io.IOException e) { System.out.println(e); } } // end of main } // end of class