Fibonacci Sequence on Rosetta Code

Post Reply
User avatar
sarossell
Posts: 195
Joined: Sat Nov 05, 2016 6:31 pm
My devices: iPad Mini 2, iPhone 5, MacBook Air, MacBook Pro
Flag: United States of America
Contact:

Fibonacci Sequence on Rosetta Code

Post by sarossell »

Just posted code on Rosetta Code for the Fibonacci Sequence. As a fan of mathematics and number theory, it's a personal favorite. The other solutions all use Iterative and Recursive methods while ignoring the much simpler Binet's Formula for direct N-th term calculation, so I included all three methods.

http://rosettacode.org/wiki/Fibonacci_s ... mart_BASIC

As always, if you see a way to simplify or optimize my code, please let me know. I'm all about saving bytes.
smart BASIC Rocks!

- Scott : San Diego, California

Henko
Posts: 822
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Fibonacci Sequence on Rosetta Code

Post by Henko »

I like short, efficient code too.

Def fib(n) = int(exp(.481212*n-.80472))

Accurate for n = 1 through 26

User avatar
sarossell
Posts: 195
Joined: Sat Nov 05, 2016 6:31 pm
My devices: iPad Mini 2, iPhone 5, MacBook Air, MacBook Pro
Flag: United States of America
Contact:

Re: Fibonacci Sequence on Rosetta Code

Post by sarossell »

Clever! I've never seen that one before. I recognize the inverse hyperbolic sine of 0.5, but the other number? sine 53° 35'? Curious. Do you have an info on it's origins? And why it plays out 26? Would more accurate sine values improve accuracy to 100%?
smart BASIC Rocks!

- Scott : San Diego, California

Henko
Posts: 822
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Fibonacci Sequence on Rosetta Code

Post by Henko »

I simply tryed a couple of power funtions for fib(n), using Excel, a least squares method, and Excel's "solver" function, and got a good result wth:
Fib(n) = a*b^n. Knowing the dominant role of the square root of 5 with respect to the fibonacci series (and the "golden ratio" for that matter), the constants a and b were found to be: a=1/sqrt(5) and b=(1+sqrt(5))/2 (b happens to be the "golden ratio")

Hence: fib(n) = (((1+sqrt(5))/2)^n)/sqrt(5)
This formula is not as exact as Binet's formula for all values of n.
The INT function is needed to generate correct values from n=1 through 16. Above that value, the formula gives correct results, due to internal rounding by my (Windows) computer.

Taking natural logarithms and then converting back to an "e-power" and substituting numbers for a and b, result in the formula in my earlier post. The accuracy is determined by the number of decimals. Given a desired level of accuracy, the needed number of decimals can be given (until stopped by the internal accuracy of the computer).

BTW: i read on internet that Binet's formula was already mentioned by De Moivre about 100 years earlier! The fraud!!

User avatar
sarossell
Posts: 195
Joined: Sat Nov 05, 2016 6:31 pm
My devices: iPad Mini 2, iPhone 5, MacBook Air, MacBook Pro
Flag: United States of America
Contact:

Re: Fibonacci Sequence on Rosetta Code

Post by sarossell »

Yeah, history is rife with nationalist B.S. about inventions and discoveries. Many of Da Vinci's discoveries are suspect. No one can agree on who invented radio, television, the electric car, the first car, and even the first airplane. It's crazy.
smart BASIC Rocks!

- Scott : San Diego, California

User avatar
Dav
Posts: 279
Joined: Tue Dec 30, 2014 5:12 pm
My devices: iPad Mini, iPod Touch.
Location: North Carolina, USA
Contact:

Re: Fibonacci Sequence on Rosetta Code

Post by Dav »

Wow, I didn't realize you could use DEF in such a way. By using that method I can make what we call CONST variables in QB64 (a variable whose value stays constant throughout the code). And using them in Functions I don't have to put them in scope (.) syntax either. Here's what I mean...

Code: Select all

def sd=123

printsd

def printsd
   print sd  'should show 123, even whithout scope (.)
end def
Cool. Thanks Henko.

- Dav

Post Reply