Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2021
806
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. package StackAndQueue;
  2. import java.util.*;
  3. public class RecursiveFibonacci {
  4. public static void main(String[] args) {
  5. Scanner input = new Scanner(System.in);
  6. ArrayDeque<Long> Fibonacci = new ArrayDeque<>();
  7. long N = Integer.parseInt(input.nextLine());
  8. System.out.println(getFibonacci(Fibonacci, N));
  9. }
  10.  
  11. private static Long getFibonacci(ArrayDeque<Long> Fibonacci, Long N) {
  12. if(N ==1 || N == 2){
  13. return 1L;
  14. }else {
  15. Fibonacci.offer(0L);
  16. Fibonacci.offer(1L);
  17. for (int i = 0; i < N; i++) {
  18. long Sum = Fibonacci.poll()+Fibonacci.peek();
  19. Fibonacci.offer(Sum);
  20. }
  21. }
  22. Fibonacci.poll();
  23. return Fibonacci.peek();
  24. }
  25. }
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement