baby buddha ([info]amisplacedphile) wrote in [info]coders_haven,
@ 2003-10-22 10:17:00
Previous Entry  Add to memories!  Tell a Friend!  Next Entry
Fibonacci Sequence
EDIT:
Got the coding worked out now, thanks for everyone's help!

Late thx for all the help with QBasic.

I'm going crazy trying to get this fibonacci sequence to work right in Visual Basic.

Most sites give me the formula, but now I'm trying to understand it, and it's just not coming out right.
Anyone got any advice/help?


The program needs to generate a selected Fibonacci number (i.e. 1 = 1, 2 = 1. 3 = 2, 4 = 3...etc.)

The general formula is Fibonacci = Fibonacci(Number-1)+Fibonacci(Number-2)

Can anyone help please? Reply if you need additional details, etc...



(Post a new comment)


[info]heptadecagram
2003-10-22 08:01 am UTC (link)

Some code of what you have so far would help. Or, at least, output. What happens when you give the program an input of 1? Or 3? Or -1?

I'm trying to recall (away from my QBasic interpreter) if QBasic allows recursive functions. This could be your problem.

(Reply to this)


[info]khalidz0r
2003-10-22 09:14 am UTC (link)
I do not do VB, so I cannot give you specefic code, but the best way to solve fibonacci is through dynamic programing. Here is psudocode for it, if you do not understand the algorithm, I can help, but if you don't know how to implement it in VB, you'd have to ask a VB programer. I loathe VB ;)

procedure fib(N)
declare integer array A[1..N]
A[1] := 1
A[2] := 1
for I = 3 to N by 1
A[N] := A[N-1] + A[N-2]
end fib

Good luck :)

(Reply to this) (Thread)


[info]amisplacedphile
2003-10-23 09:23 pm UTC (link)
That helped a LOT!

I finally realized I wasn't assigning a range for it...(the I = 3 to N)

...and I totally screwed up the formula.

(Reply to this) (Parent)


[info]mattlyle
2003-10-22 02:54 pm UTC (link)
that is one way to compute the formula, but not the only. there is also a constant-time formula:

F(n) = ( a^n - b^n ) / ( a - b )
where
a = ( 1 + sqrt( 5 ) ) / 2
b = ( 1 - sqrt( 5 ) ) / 2

so calculating large numbers of the sequence is much faster using this method.

(Reply to this)


[info]seriouslyuguys
2003-10-23 05:19 am UTC (link)
using the constant time formula probably won't teach you what you are supposed to learn from this assignment. What you need to know is that each number of the sequence is the sum of the previous two, so to get any given number of the sequence you need to calculate all of the previous numbers.

So to get the fifth number.

sum the first and second to get the third 1+1=2

then sum the second and third to get the fourth 1+2=3

then sum the third and fourth to get the fifth 2+3=5

and we stop and return 5 as the fifth number.

(Reply to this)


Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…