G
G
Goddamnboy2019-05-08 07:55:08
Algorithms
Goddamnboy, 2019-05-08 07:55:08

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

4 answer(s)
T
Tigran Abrahamyan, 2019-05-10
@Goddamnboy

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;
    }

JavaScript code.

M
Makssof, 2019-05-08
@makssof

wikipedia.org/wiki/Fibonacci_numbers
Knowing what the fibonacci sequence is, you can understand the algorithm

P
Pavel, 2019-05-08
@HEKOT

In addition to the quite exhaustive answer from makssof :
Fibonacci in an interview

H
hint000, 2019-05-08
@hint000

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 question

Ask a Question

731 491 924 answers to any question