• We should now be fully online following an overnight outage. Apologies for any inconvenience, we do not expect there to be any further issues.

Anyone here understand Fibonacci numbers?

RMSistight

Golden Member
Oct 2, 2003
1,740
0
0
So I created a code to calculate Fibonacci numbers in my program in a loop. This is essentially it:

A= 1
B= 1

C = A + B

A = B

B = C

The loop works to display the correct numbers (1, 1, 2, 3, 5, 8, 13, 21, etc) but I don't understand the coding...someone please explain!
 

n0cmonkey

Elite Member
Jun 10, 2001
42,936
1
0
What don't you get exactly? :p

First number + second number = third number + second number = forth number + third number = fifth number + forth number = sixth number + fifth number = seventh number + sixth number...
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Stupid $ signs :p

<?php

$a = 1;
$b = 1;
for($i = 0; $i < 100; $i++)
{
$c = $a + $b;
$a = $b;
$b = $c;
echo $c . "\n";
}

?>
 

RMSistight

Golden Member
Oct 2, 2003
1,740
0
0
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.
 

n0cmonkey

Elite Member
Jun 10, 2001
42,936
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.

Read my edit.
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: n0cmonkey
Originally posted by: RMSistight
Originally posted by: n0cmonkey
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.

Read my edit.

Thanks man. Much obliged.

English > code :cool:

Yes

 

RMSistight

Golden Member
Oct 2, 2003
1,740
0
0
Originally posted by: n0cmonkey
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.

Read my edit.

Ok I read your edit...and still having trouble grasping it. I'm sorry for being an idiot but could you explain it in English line for line of the code?

From what I understand is:

1 + 1 = C so C = 2

A = B (1 = 1?)

B = C (1 = 2?)

 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: RMSistight
Originally posted by: n0cmonkey
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.

Read my edit.

Ok I read your edit...and still having trouble grasping it. I'm sorry for being an idiot but could you explain it in English line for line of the code?

From what I understand is:

1 + 1 = C so C = 2

A = B (1 = 1?)

B = C (1 = 2?)

Pretty much
 

RMSistight

Golden Member
Oct 2, 2003
1,740
0
0
Originally posted by: amdfanboy
Originally posted by: RMSistight
Originally posted by: n0cmonkey
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.

Read my edit.

Ok I read your edit...and still having trouble grasping it. I'm sorry for being an idiot but could you explain it in English line for line of the code?

From what I understand is:

1 + 1 = C so C = 2

A = B (1 = 1?)

B = C (1 = 2?)

Pretty much


OH I GET IT NOW. So:

1 + 1 = C

C = 2

So A = B (1 = 1) This make the original equation subtitute A for B in A + B = C

Then

B = C makes the substitute for B to C which in the end makes the new equation B + C = new number yes?
 

n0cmonkey

Elite Member
Jun 10, 2001
42,936
1
0
Originally posted by: RMSistight
Originally posted by: n0cmonkey
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.

Read my edit.

Ok I read your edit...and still having trouble grasping it. I'm sorry for being an idiot but could you explain it in English line for line of the code?

From what I understand is:

1 + 1 = C so C = 2

A = B (1 = 1?)

B = C (1 = 2?)

You're making this difficult. :p

Ok, so 1 + 1 = 2. Now A is the same as B. We then change B to equal C in our A + B = C equation.

Kind of in order (what is in () is the letter that represented the number in the previous equation):
1. A = 1, B = 1, C = 2
2. A = 1(B), B = 2(C), C = 3
3. A = 2(B), B = 3(C), C = 5
etc.
 

n0cmonkey

Elite Member
Jun 10, 2001
42,936
1
0
Originally posted by: RMSistight
Originally posted by: amdfanboy
Originally posted by: RMSistight
Originally posted by: n0cmonkey
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.

Read my edit.

Ok I read your edit...and still having trouble grasping it. I'm sorry for being an idiot but could you explain it in English line for line of the code?

From what I understand is:

1 + 1 = C so C = 2

A = B (1 = 1?)

B = C (1 = 2?)

Pretty much


OH I GET IT NOW. So:

1 + 1 = C

C = 2

So A = B (1 = 1) This make the original equation subtitute A for B in A + B = C

Then

B = C makes the substitute for B to C which in the end makes the new equation B + C = new number yes?

It's late for me, but that sounds about right. Everything just shifts down one spot.
 

RMSistight

Golden Member
Oct 2, 2003
1,740
0
0
Originally posted by: n0cmonkey
Originally posted by: RMSistight
Originally posted by: amdfanboy
Originally posted by: RMSistight
Originally posted by: n0cmonkey
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.

Read my edit.

Ok I read your edit...and still having trouble grasping it. I'm sorry for being an idiot but could you explain it in English line for line of the code?

From what I understand is:

1 + 1 = C so C = 2

A = B (1 = 1?)

B = C (1 = 2?)

Pretty much


OH I GET IT NOW. So:

1 + 1 = C

C = 2

So A = B (1 = 1) This make the original equation subtitute A for B in A + B = C

Then

B = C makes the substitute for B to C which in the end makes the new equation B + C = new number yes?

It's late for me, but that sounds about right. Everything just shifts down one spot.


Thanks for your help man. I appreciate it.
 

n0cmonkey

Elite Member
Jun 10, 2001
42,936
1
0
Originally posted by: RMSistight
Originally posted by: n0cmonkey
Originally posted by: RMSistight
Originally posted by: amdfanboy
Originally posted by: RMSistight
Originally posted by: n0cmonkey
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.

Read my edit.

Ok I read your edit...and still having trouble grasping it. I'm sorry for being an idiot but could you explain it in English line for line of the code?

From what I understand is:

1 + 1 = C so C = 2

A = B (1 = 1?)

B = C (1 = 2?)

Pretty much


OH I GET IT NOW. So:

1 + 1 = C

C = 2

So A = B (1 = 1) This make the original equation subtitute A for B in A + B = C

Then

B = C makes the substitute for B to C which in the end makes the new equation B + C = new number yes?

It's late for me, but that sounds about right. Everything just shifts down one spot.


Thanks for your help man. I appreciate it.

Not a problem. For some reason I was thinking about Fibonacci numbers earlier today. And it's one of the few math questions I can answer. :p
 

Apathetic

Platinum Member
Dec 23, 2002
2,587
6
81
Originally posted by: RMSistight
Originally posted by: n0cmonkey
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.

Read my edit.

Ok I read your edit...and still having trouble grasping it. I'm sorry for being an idiot but could you explain it in English line for line of the code?

From what I understand is:

1 + 1 = C so C = 2

A = B (1 = 1?)

B = C (1 = 2?)

A, B, and C are "variables". That is, their values vary over time. Rather than reading "A equals B", read it as "A gets assigned the value of B".

Remember, the values of A, B, and C will change each pass through the loop!

See if this helps:
--------------------------------------
A = 1
B = 1

Print A
Print B

For I = 1 To 5
C = A + B
A = B
B = C
Print C
Next
--------------------------------------

Here are our values when we start:
A= 1 (A is assigned the value "1")
B= 1 (B is assigned the value "1")
(C is currently undefined)

In our first pass of the for loop we have this:
C = A + B (C is assigned the value 1 + 1, which is 2)
A = B (A is assigned the value of B, which is 1)
B = C (B is assigned the value of C, which is 2)
We print C, which is "2"

In the second pass of the loop:
C = A + B (C is assigned the value 1 + 2, which is 3)
A = B (A is assigned the value of B, which is 2)
B = C (B is assigned the value of C, which is 3)
We print C, which is "3"

In the third pass of the loop:
C = A + B (C is assigned the value 2 + 3, which is 5)
A = B (A is assigned the value of B, which is 3)
B = C (B is assigned the value of C, which is 5)
We print C, which is "5"

In the fourth pass of the loop:
C = A + B (C is assigned the value 3 + 5, which is 8)
A = B (A is assigned the value of B, which is 5)
B = C (B is assigned the value of C, which is 8)
We print C, which is "8"

In the fifth pass of the loop:
C = A + B (C is assigned the value 5 + 8, which is 13)
A = B (A is assigned the value of B, which is 8)
B = C (B is assigned the value of C, which is 13)
We print C, which is "13"

We get the following output
1
1
2
3
5
8
13

I hope this helps.

Dave

Edit: dumb typo
 

littleprince

Golden Member
Jan 4, 2001
1,339
1
81
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.
 

Kyteland

Diamond Member
Dec 30, 2002
5,747
1
81
just use the direct function

f(n) = (1/2)*( (1-1/sqrt(5))*((1-sqrt(5))/2)^n + (1+1/sqrt(5))*((1+sqrt(5))/2)^n )

I can show you how to derive that if you want.
 

DingDingDao

Diamond Member
Jun 9, 2004
3,044
0
71
Originally posted by: Kyteland
just use the direct function

f(n) = (1/2)*( (1-1/sqrt(5))*((1-sqrt(5))/2)^n + (1+1/sqrt(5))*((1+sqrt(5))/2)^n )

I can show you how to derive that if you want.

:Q I'm not the OP, but I would definitely like to see the derivation of this function

EDIT: Ran this sequence through an Excel spreadsheet, and I'm getting 1, .27, 4.79, -6.30, 35.27, -90.06, 330.40, -1006.18, 3358.05, -10701.90, 34898.97...

Are your parentheses placed properly? These are values calculated using standard order of operations.

Or did you just make that function up? If so, you got me ;)
 

Kyteland

Diamond Member
Dec 30, 2002
5,747
1
81
That function is real. I placed it in excel and got this:
0 1
1 1
2 2
3 3
4 5
5 8
6 13
7 21
8 34
9 55
10 89
11 144
12 233
13 377
14 610
15 987

With my equation being this:
=(1/2)*( (1-1/SQRT(5))*((1-SQRT(5))/2)^A1 + (1+1/SQRT(5))*((1+SQRT(5))/2)^A1)

The function is a simple recurrance relation and can be solved through a number of methods. I will post one later when I get some time to write it all ip.

 

Alphathree33

Platinum Member
Dec 1, 2000
2,419
0
0
First, there is a constant-time algorithm for calculating the n-th fibbi number involving the golden ratio.

Secondly, this is a retardedly easy problem that you shouldn't need help on. The fact that you do says the following:
You suck at math and/or you suck at computer science

Go find something you're actually good at and do that.

And stop copying your friend's homework.
 

Kyteland

Diamond Member
Dec 30, 2002
5,747
1
81
The solution using the annihilator method:

1) f(n) = f(n-1)+f(n-2)
f(n)- f(n-1) - f(n-2) = 0

2) <E^2-E-1> = 0
If you don't get this step, see google for more info on the annihilator method of solving recurrances

3) <E-((1-sqrt(5))/2><E-((1+sqrt(5)/2)> = 0

4) f(n) = C1*(((1-sqrt(5))/2)^n + C2*(((1+sqrt(5))/2)^n
f(0) = 1
f(1) = 1

5) Solve for the unknown C1 and C2
C1 = 1/2*(1-1/sqrt(5))
C2 = 1/2*(1+1/sqrt(5))

So the solved recurrance is:
f(n) = (1/2)*( (1-1/sqrt(5))*((1-sqrt(5))/2)^n + (1+1/sqrt(5))*((1+sqrt(5))/2)^n )