Well /g/ i might be completely retarded:
Long story short: i stumbled upon this piece of Gamemaker codedef FibonacciNum(N):
if N <= 1:
return 1
else:
return FibonacciNum(N - 1) + FibonacciNum(N - 2)
I then tried multiple times to trace it's working (been years since i've touched code) through notepad (writing down what would have been returned from each recursion) but i never managed to get it right.
Looking for "Fibonacci function" i ended up on this (slightly modified to take the standard input directly instead of a for cycle from 0 to n)#include<stdio.h>
int Fibonacci(int);
main()
{
int n, i = 0, c;
scanf("%d",&n);
printf("Fibonacci series\n");
printf("%d\n", Fibonacci(n));
return 0;
}
int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}
Trying it through ideone i get that Fibonacci(3) gets me 2 and Fibonacci(5) gets me 5
Can someone help a retard understand this stuff?
No >install gentoo please.
Thread soundtrack: https://www.youtube.com/watch?v=2WaHCUB3QX0
What, read up on recursion.
>>51565085
>tree recursion
this isn't halal
Are you trying to understand what a fibonacci sequence is, or what recursive functions are?
>>51565229
I did but this whole thing stumps me.
Example: Fibonacci (5)
Returns 5-1=4 and 5-2=3
For the 3 it's all OK, it works, i can trace it back (it becomes 3-1=2 that in turns becomes 1+0 (and the 0->1) and 3-2=1)
Is the 4 that fucks me up
>>51565327
Read SICP
>>51565327
draw a box for each call to fibonnacci and also write down in the box the value passed to the functions argument
remember that it only bottoms out when the argument n is 0 or 1 otherwise it calls the function again (two times, one for n-1 and one for n-2)
>>51565309
The fact that i know what a fibonacci sequence is and (more or less) what recursions are make me worry that, since i can't understand a relatively simple algorithm, i've well and truly become retarded.
Not Down's Syndrome retarded, worse.
>>51565378
I did with the first algorithm, the 3 worked perfectly but the 5 became, somehow, a 8 when i went "back up"
>>51565409
the C one looks like it is 0 indexed anyways.
first number is #0
second number is #1
third is #2
if that is a problematic part
so to get the 8th number one would pass 7 to the c program I believe.
>>51565489
(that is if you wish your initial two numbers to be 0,1 rather than 1,1)
>>51565508
>>51565489
Well, looks like if i take the zeros as zeros it adds up.
However the other one whereif(n<=0)stumps me just as before
return 1;