[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Well /g/ i might be completely retarded: Long story short: i
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 12
Thread images: 1
File: 312.jpg (71 KB, 320x240) Image search: [Google]
312.jpg
71 KB, 320x240
Well /g/ i might be completely retarded:

Long story short: i stumbled upon this piece of Gamemaker code
def 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 where
if(n<=0)
return 1;
stumps me just as before
Thread replies: 12
Thread images: 1

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.