Quick actions

cmd+k|ctrl+k

Navigation

Languages

Fibonacci done via Iteration and Recursion

Snippet info

Language

JavaScript

Visibility

public

Author

dhamankovachi1

Created

2023-12-01T16:31:33.769446Z

Updated

2023-12-01T16:32:02.692549Z

// Given a number N return the index value of the Fibonacci sequence, where the sequence is:

// 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 ...
// the pattern of the sequence is that each value is the sum of the 2 previous values, that means that for N=5 → 2+3

//For example: fibonacciRecursive(6) should return 8

function fibonacciIterative(n){                   //O(n)-> Time Complexity
  //code here;                             
  let arr=[0,1];
  for(let i=2;i<n+1;i++){
      arr.push(arr[i-1]+arr[i-2])
  }
  return arr[n]
}
fibonacciIterative(3);

function fibonacciRecursive(n) {          //O(2^n)-> Time Complexity
  //code here;
  if(n<2){
      return n;
  }
  return fibonacciRecursive(n-1)+fibonacciRecursive(n-2);
}

fibonacciRecursive(3)
INFO