Thursday 13 February 2014

UVA sloved: problem number: 10579 - Fibonacci Numbers

10579 - Fibonacci Numbers
Time limit: 3.000 seconds

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

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.*/

No comments:

Post a Comment