I
I
Insolation2018-03-13 17:23:07
JavaScript
Insolation, 2018-03-13 17:23:07

How to calculate fibonacci numbers without recursion?

We need to write a function that will return an array of Fibonacci numbers without recursion.

This is where I got stuck. As a matter of fact if to do with a recursion that turns out at me such.

const fibo = n => {
  let fib = [1, 1];
  for (let i = 2; i < n; i++) {
    fib[i] = fib[i - 2] + fib[i - 1];
  }
  return fib;
};

But how to do without it, something I can’t put my mind to.
Advise the algorithm please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
twobomb, 2018-03-13
@Insolation

function fibb(n){
    var arr = [0,1];
  for(var i = 0; i < n-2;i++)
  	arr.push(arr[i]+arr[i+1]);
    return arr;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question