Advertisement
Tsuki11

Untitled

May 21st, 2020
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class Exercise_Task_7_RecursiveFibonacci {
  6. //dynamic optimization and memorization
  7. private static int counter = 0;
  8. private static long[] memory;
  9.  
  10. public static void main(String[] args) throws IOException {
  11. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  12.  
  13. int n = Integer.parseInt(reader.readLine());
  14.  
  15. memory = new long [n + 1];
  16.  
  17. long result = fibonacci(n);
  18.  
  19. System.out.println(result);
  20. //System.out.println(counter);
  21. }
  22.  
  23. private static long fibonacci(int n) {
  24. counter ++;
  25. if(n < 2){
  26. return 1;
  27. }
  28.  
  29. if(memory[n] !=0){
  30. return memory[n];
  31. }
  32.  
  33. return memory[n] = fibonacci(n - 1) + fibonacci(n - 2);
  34. }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement