# Can't calculate the Fibonacci number, there is someone strong on this issue?

Is not so much to calculate how many you want to understand how it is calculated.
Task: Implement a function fib which Fibonacci numbers. A function argument is the ordinal number.
f(0) = 0
f(1) = 1
f(n) = f(n-1) + f(n-2)

Example:
2 == fib(3) // if formula then there must be fib(3) = f(n-1) + f(n-2) = f(2) + f(1) = 2+1=3, how 2 got ???
5 == fib(5) // then what is 5 ???
55 == fib(10)
July 9th 19 at 11:19
July 9th 19 at 11:21
Solution
f(2) = f(1) + f(0) = 1. Here's where you went wrong. So

f(3) = f(2) + f(1) = 1 + 1 = 2;
f(4) = f(3) + f(2) = 2 + 1 = 3;
f(5) = f(4) + f(3) = 3 + 2 = 5;
f(6) = f(5) + f(4) = 5 + 3 = 8;
f(7) = f(6) + f(5) = 8 + 5 = 13;
July 9th 19 at 11:23
You need to learn the concept of recursion.
"2 as it turned out" - as f(2) = f(n-1) + f(n-2)
Only you have the error, fib(3) = 2 and not 3
Code codepen.io/anon/pen/ezOxMQ?editors=1111
July 9th 19 at 11:25
Apparently the problem is in Your function, try:

``````function fibonacci(\$n)
{
if (\$n < 3) {
return 1;
}
else {
return fibonacci(\$n-1) + fibonacci(\$n-2);
}
}

for (\$n = 1; \$n <= 16; \$n++) {
echo(fibonacci(\$n) . ", ");
}
echo("...\n")``````

progopedia.ru/example/fibonacci/89

Find more questions by tags JavaScriptPHP