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 100Sample Output
2 354224848179261915075
Note: No generated fibonacci number in excess of 1000 digits will be in the test data, i.e. f (20) = 6765 has 4 digits..
Local_UVa'2003
_____________________________________________________________________solution:
import java.math.BigInteger;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner input= new Scanner(System.in);
int x;
while(input.hasNext())
{
x=input.nextInt();
BigInteger h=BigInteger.ONE;
BigInteger m=BigInteger.ONE;
BigInteger sum=BigInteger.ZERO;
while(x>2)
{
sum=h.add(m);
m=h;
h=sum;
x--;
}
System.out.println(sum);
}
}
}
/* had a little help from a big pal.*/