Tuesday, June 9, 2015

UVa 495 - Fibonacci Freeze

// UVa 495 - Fibonacci Freeze

import java.math.BigInteger;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		BigInteger[] f = new BigInteger[5001];
		f[0] = BigInteger.ZERO;
		f[1] = BigInteger.ONE;
		for (int i = 0; i <= 4998; i++)
			f[i + 2] = f[i + 1].add(f[i]);

		Scanner scan = new Scanner(System.in);
		while (scan.hasNextInt()) {
			int n = scan.nextInt();
			System.out.println("The Fibonacci number for " + n + " is " + f[n]);
		}
	}

}

No comments:

Post a Comment