deyanmalinov

07. Recursive Fibonacci

Jul 25th, 2019
115
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.     private static long[] memory;
  5.     public static void main(String[] args) {
  6.         Scanner scan = new Scanner(System.in);
  7.         int number = Integer.parseInt(scan.nextLine());
  8.         memory = new long[number+1];
  9.         long res = getFib(number);
  10.         System.out.println(res);
  11.     }
  12.  
  13.     private static long getFib(int number) {
  14.         if (number < 2){
  15.             return 1;
  16.         }
  17.         if (memory[number] !=0){
  18.             return memory[number];
  19.         }
  20.         return memory[number] = getFib(number-1) + getFib(number-2);
  21.     }
  22. }
RAW Paste Data