238025681

Untitled

May 20th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package stackandqueues;
  7.  
  8. import java.math.BigInteger;
  9. import java.util.ArrayDeque;
  10. import java.util.ArrayList;
  11. import java.util.Scanner;
  12.  
  13. /**
  14.  *
  15.  * @author kalin
  16.  */
  17. public class O8_StackAndQueues {
  18.  
  19.     public static void main(String[] args) {
  20.         Scanner scanner = new Scanner(System.in);
  21.         String input = scanner.nextLine();
  22.         int nFibonacci = Integer.parseInt(input);
  23.        
  24.         System.out.println(getFibonacci(nFibonacci, new BigInteger[nFibonacci + 1]));
  25.  
  26.     }
  27.  
  28.     private static BigInteger getFibonacci(int nFibonacci,BigInteger[] fibonacciNumber) {
  29.        
  30.         if (nFibonacci == 0 || nFibonacci == 1) {
  31.             return BigInteger.valueOf(1);
  32.  
  33.         }
  34.         BigInteger  temp = fibonacciNumber[nFibonacci];
  35.         if (temp == BigInteger.ZERO || temp == null) {
  36.             temp = getFibonacci(nFibonacci - 2, fibonacciNumber).add(getFibonacci(nFibonacci - 1, fibonacciNumber));
  37.             fibonacciNumber[nFibonacci] = temp;
  38.         }
  39.  
  40.         return temp;
  41.     }
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment