Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class Exercise_Task_7_RecursiveFibonacci {
- //dynamic optimization and memorization
- private static int counter = 0;
- private static long[] memory;
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- int n = Integer.parseInt(reader.readLine());
- memory = new long [n + 1];
- long result = fibonacci(n);
- System.out.println(result);
- //System.out.println(counter);
- }
- private static long fibonacci(int n) {
- counter ++;
- if(n < 2){
- return 1;
- }
- if(memory[n] !=0){
- return memory[n];
- }
- return memory[n] = fibonacci(n - 1) + fibonacci(n - 2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement