Anyone here understand Fibonacci numbers?

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.

Soccer55

Golden Member
Jul 9, 2000
1,660
4
81
The Fibonacci numbers are recursively defined: For notation, let F(n) stand for the nth Fibonacci number. Then the recursive definition is: F(n+2) = F(n+1) + F(n) where F(1) = 1 and F(2) = 1.

So for example, in order to find the 4th Fibonacci number, we have that F(4) = F(3) + F(2).
But F(3) = F(2) + F(1) where F(2) = 1 and F(1) = 1.
So after substitution, we have that F(4) = F(2) + F(1) + F(2) = 1 + 1 + 1 = 3.

Think of it that way and it might help you understand the Fibonacci sequence and then the code.

-Tom

Edited for clarity
 

Kyteland

Diamond Member
Dec 30, 2002
5,747
1
81
Originally posted by: zerocool1
is there an implementation that's recursive...I've only seen iterative implementations of it.

int FibRecursive( int index )
{
if ( index > 1 )
{
return (FibRecursive(index-1) + FibRecursive(index-2));
}

else
{
return (1);
}
}

This can be sped up significantly if you store each f(n) in an array the first time you calculate it.
 

HonkeyDonk

Diamond Member
Oct 14, 2001
4,020
0
0
as someone stated before, i think your biggest confusion was the A=B, B=C part.

It may look like A equals B, but in reality, the programming language is saying A is given the value of whatever B is.

if you think of it that way, you'll begin to see it more clearly.
 

shuan24

Platinum Member
Jul 17, 2003
2,558
0
0
Give the guy a break you code nazis. ATOT is just as good of a resource as any other for understanding programming material. I'm sure learning code was sooo easy for the rest of you guys. /rolleyes

OMG L33T H@X0R!!!!
 

calboy98

Member
Jun 4, 2003
74
0
0
if there's a book for this course, read the first chapter or 2. usually explains syntax and basics of the language. also lookup variable in the index.
 

RMSistight

Golden Member
Oct 2, 2003
1,740
0
0
Originally posted by: littleprince
I know i'm gonna get flamed for this, but this is my values, and I stand 100% behind it.

This is obviously some kinda assignment. Your friend obviously gave you the code which you admit. You obviously have done little research on it, or have little understanding of it.

Perhaps you shouldn't submit it? At least it gives you a true understanding of where you are instead of falsely inflating your marks. It'll get you no where in life as if this is for a programming class it looks to be a pretty simple thing.

All my friend explained to me was the loop code. I figured out the rest of the program. There are also a lot of other students having the same problems as I am with this. I just thank God I have a kick ass friend who is really good at math help me understand this better.

Also, this is not a CS course, it's a business programming course. I'm not trying to become a programmer.
 

AyashiKaibutsu

Diamond Member
Jan 24, 2004
9,306
4
81
Basicly you add A and B and put it in C. Now you need to add B and C and rather then using new variables you put be in the useless A and C into B (which is useless because it's value is now in A). Then you repeat. You could alternate c=a+b and then a=b+c and keep alternating your code will be a little more effecient, but not by much since you have to keep track of which one your on (if else statement instead of 2 moves).
 

bubbadu

Diamond Member
Aug 30, 2001
3,551
0
0
Heres how you do it in Ocaml :p with recursion.

let rec fib n =
match n with
0->1
|1->1
|_->fib(n-1)+fib(n-2)

 

ZoNtO

Diamond Member
Sep 27, 2003
3,709
0
0
www.rileylovendale.com
an = sign in code doesn't mean "equal to" it means "assigned the value of". I didn't think you could code numerical constants as variables anyway........

I'm too lazy to go through and actually think about it, I just found out I got a 77 on my CS exam and I'm pissed right now....
 

Kyteland

Diamond Member
Dec 30, 2002
5,747
1
81
Originally posted by: Kyteland
Originally posted by: bubbadu
Heres how you do it in Ocaml :p with recursion.

let rec fib n =
match n with
0->1
|1->1
|_->fib(n-1)+fib(n-2)
Yes, but how would you go about doing it in BrainFuck?
Found it. ;)

+++++++++++>+>>>>++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+>+<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[-<-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<<-]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<+>>[-]]<<<<<<<]>>>>>[++++++++++++++++++++++++++++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++++++++++++++++++++++++++++++++++++++++++++++.[-]<<<<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<<[-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]
>[<<+>>[-]]<<<<<<<]>>>>>[+++++++++++++++++++++++++
+++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++
++++++++++++++++++++++++++++++++++++++++++++.[-]<<
<<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<<
[-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
Originally posted by: RMSistight
Originally posted by: n0cmonkey
What don't you get exactly? :p

Well..the part where it says:

C = A + B

A = B

B = C

My friend told me to write it out...which I did...but I don't understand it.

what are you retarded? you wrote it out and you dont understand it?


please kill yourself
 
Aug 16, 2001
22,505
4
81
Originally posted by: Kyteland
Originally posted by: Kyteland
Originally posted by: bubbadu
Heres how you do it in Ocaml :p with recursion.

let rec fib n =
match n with
0->1
|1->1
|_->fib(n-1)+fib(n-2)
Yes, but how would you go about doing it in BrainFuck?
Found it. ;)

+++++++++++>+>>>>++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+>+<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[-<-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<<-]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<+>>[-]]<<<<<<<]>>>>>[++++++++++++++++++++++++++++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++++++++++++++++++++++++++++++++++++++++++++++.[-]<<<<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<<[-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]
>[<<+>>[-]]<<<<<<<]>>>>>[+++++++++++++++++++++++++
+++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++
++++++++++++++++++++++++++++++++++++++++++++.[-]<<
<<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<<
[-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]

LOL WTF?
 

DivideBYZero

Lifer
May 18, 2001
24,117
2
0
Ahh. now you got me getting all into BrainFuck*.




*This is the name of the language, so technically, I am not cussing.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
How fvcking hard is it?

write down two numbers:

1, 1

add then together.

2.

Write that at the end of the last two numbers you had.

1, 1, 2

Take the last two numbers from that line and add them together

1 + 2 = 3

Write that number at the end of the line

1, 1, 2, 3

Add the last two numbers again

2 + 3 = 5

Write that at the end of the line:

1, 1, 2, 3, 5

Repeat indefinitely.

It's freaking addition... how is it confusing?