UVA – 495 – Fibonacci Freeze

import java.math.BigInteger;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		BigInteger[] fib = fib(5001);
		Scanner in= new Scanner(System.in);
		while(in.hasNext()){
			int n = in.nextInt();
			System.out.printf("The Fibonacci number for %d is %d\n", n, fib[n] );
		}
	}

	public static BigInteger[] fib(int n) {
		BigInteger[] fib = new BigInteger[n + 1];
		fib[0] = BigInteger.ZERO;
		fib[1] = BigInteger.ONE;
		for (int i = 2; i <= n; i++) 
			fib[i]= fib[i-1].add(fib[i-2]);
		return fib;
	}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: