Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Fibonacci Series

Author
Message
hexGEAR
21
Years of Service
User Offline
Joined: 3rd Nov 2002
Location: Naytonia
Posted: 28th Apr 2003 20:01
ok, i was doing some of my exam revision and came across this seires, it goes like this 1,1,2,3,5,8,13,21,34

now the logic behind it is
Fib1 = 1
Fib2 = 1
Fibn = (Fibn-2) + (Fibn-1)

i tried coding this in dark basic:

---------------------------------------------------------------------
sync on
n = 5
do
set cursor 200,200:print fib(n)
sync
loop

function fib(n)
if n > 2 then exitfunction fib(n-2) + fib(n-1)
endfunction 1
---------------------------------------------------------------------

for some reason fib(5) gives me 4 instead of 5, i'm sure i made a mistake somewhere but can't find where! help someone!
everyday of life is a new chapter that has already been fortold but is up to the soul to capture.
freak
21
Years of Service
User Offline
Joined: 20th Jan 2003
Location:
Posted: 28th Apr 2003 20:15


if you run this code, you see 20 results a method I just invented (by first filling an array)
if you press enter, you see 20 results of your method

like you see, your row of fibonnacci just starts with 1,1,2,... and then it just adds one number each time

I don't know why... I never used exitfunction (I didn't know it existed) and I even didn't know that you could place an integer after endfunction...

Anyway, my method is as fast as yours as you also have to calculate all previous numers... I might also do it with three variables (so I don't have to use a large array)
hexGEAR
21
Years of Service
User Offline
Joined: 3rd Nov 2002
Location: Naytonia
Posted: 28th Apr 2003 20:29
thanks but i'm kinda looking for a way to get a particular number within the series without storing them anywhere else, using recursive techniques (function calling itself) or something thanks for the effort though

everyday of life is a new chapter that has already been fortold but is up to the soul to capture.
hexGEAR
21
Years of Service
User Offline
Joined: 3rd Nov 2002
Location: Naytonia
Posted: 28th Apr 2003 20:34
ahh thanks a lot, your layout made me realise something, for some reason adding this works:

---------------------------------
a = fib(n-2)
b = fib(n-1)
exitfunction a+b
---------------------------------

and this doesn't

---------------------------------
exitfunction fib(n-2) + fib(n-1)
---------------------------------

kinda strange

everyday of life is a new chapter that has already been fortold but is up to the soul to capture.
spooky
21
Years of Service
User Offline
Joined: 30th Aug 2002
Location: United Kingdom
Posted: 29th Apr 2003 00:29
Here is a bit of DBPro code to work out fib number WITHOUT recursion:

You may have to modify slightly for db classic



Gronda, Gronda
hexGEAR
21
Years of Service
User Offline
Joined: 3rd Nov 2002
Location: Naytonia
Posted: 29th Apr 2003 01:09
man and that actually works! nice, never knew there was a mathematical formula behind the fibonacci series! thanks!

everyday of life is a new chapter that has already been fortold but is up to the soul to capture.
MrTAToad
21
Years of Service
User Offline
Joined: 26th Aug 2002
Location: United Kingdom
Posted: 29th Apr 2003 01:21
If your interested, I've got a plug-in/DLL to calculate factorials...

Good news everyone! I really am THAT good...
http://www.nickk.nildram.co.uk/ for great plug-ins - oh my, yes!
freak
21
Years of Service
User Offline
Joined: 20th Jan 2003
Location:
Posted: 29th Apr 2003 01:27
I knew it was possible, we saw this last year during some maths class...

hmm strange indeed hexgear...
spooky
21
Years of Service
User Offline
Joined: 30th Aug 2002
Location: United Kingdom
Posted: 29th Apr 2003 01:35
I did not know it was possible, just did a couple of searches on google, read a few rather complicated descriptions on the calculations required (something about 'phi'), and then knocked up a function.

I might of known that MrTAToad would step in with a plug-in!!!!

Gronda, Gronda

Login to post a reply

Server time is: 2024-04-24 10:06:48
Your offset time is: 2024-04-24 10:06:48