2012-03-24 19:38:07Morris

[UVA][JAVA] 10579 - Fibonacci Numbers


  Problem E: Fibonacci Numbers 

A Fibonacci sequence is calculated by adding the previous two members of the sequence, with the first two members being both 1.

f (1) = 1, f (2) = 1, f (n > 2) = f (n - 1) + f (n - 2)

Input and Output 

Your task is to take numbers as input (one per line), and print the corresponding Fibonacci number.

Sample Input 

3
100

Sample Output 

2
354224848179261915075



import java.util.Scanner;
import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
int n;
Scanner keyboard = new Scanner(System.in);
BigInteger[] F = new BigInteger[5000];
F[1] = BigInteger.valueOf(1);
F[2] = BigInteger.valueOf(1);
for(n = 3; n < 5000; n++) {
F[n] = F[n-1].add(F[n-2]);
}
while(keyboard.hasNext()) {
n = keyboard.nextInt();
System.out.println(F[n]);
}
}
}