Uva 495 - Fibonacci Freeze

/*
    Uva 495 - Fibonacci Freeze
    Verdict : Accepted
     Time : 0.848
*/
import java.util.*;
import java.math.*;

class Main
{
    public static void main (String[] args)
    {
        Scanner in = new Scanner (System.in);
       
        int n = 2;
        BigInteger fibo [] = new BigInteger [5010];
       
        BigInteger f1 = fibo[0] = BigInteger.valueOf(0);
        BigInteger f2 = fibo[1] = BigInteger.valueOf(1);
       
        while (n <= 5000)
        {
            fibo [n] = f1.add(f2);
           
            f1 = f2;
            f2 = fibo[n];
           
            n++;
        }
       
        while (in.hasNext())
        {
            n = in.nextInt();
           
            System.out.println("The Fibonacci number for " + n + " is " + fibo[n]);
        }
    }
}

Comments

Popular posts from this blog

Uva 10650 - Determinate Prime

SPOJ-CMG - Collecting Mango

LeetCode Palindrome Number