Answer the question
In order to leave comments, you need to log in
Explain the Fibonacci number algorithm?
Here is the Fibonacci number algorithm in C++. Explain sequentially how and what is done!
int fib(unsigned int n)
{
if (n < 2) return n;
return fib(n - 1) + fib(n - 2);
}
/code>
Answer the question
In order to leave comments, you need to log in
First, we display the Fibonacci numbers in the sequence 0, 1, 1. We take the penultimate number and add the last number to the penultimate one. In our case, 1 + 1 = 2. We output 2. And so we continue the cycle, add the penultimate to the last, 1 + 2 = 3, then 2 + 3 = 5. I think you will figure it out yourself.
var n = Number(prompt("???"))
var a = 0;
console.log(a);
var b = 1;
console.log(b);
var c = a + b;
for (i = 0; i < n-2; i++) {
console.log(c);
a = b;
b = c;
c = a + b;
}
wikipedia.org/wiki/Fibonacci_numbers
Knowing what the fibonacci sequence is, you can understand the algorithm
In addition to the quite exhaustive answer from makssof :
Fibonacci in an interview
Without knowing what recursion is and how it works in programming, you will not be able to understand any recursive algorithm, even as simple as this one (only the recursive factorial algorithm is simpler). Without knowing how the stack works - in principle, you can understand recursion, but not completely.
So smoke recursion and stack until enlightenment.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question