Page 1 of 1

Fibonacci Sequence on Rosetta Code

Posted: Tue Dec 27, 2016 7:56 pm
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.

Re: Fibonacci Sequence on Rosetta Code

Posted: Tue Dec 27, 2016 11:42 pm
by Henko
I like short, efficient code too.

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

Accurate for n = 1 through 26

Re: Fibonacci Sequence on Rosetta Code

Posted: Tue Dec 27, 2016 11:52 pm
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%?

Re: Fibonacci Sequence on Rosetta Code

Posted: Wed Dec 28, 2016 10:49 am
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!!

Re: Fibonacci Sequence on Rosetta Code

Posted: Wed Dec 28, 2016 4:51 pm
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.

Re: Fibonacci Sequence on Rosetta Code

Posted: Wed Dec 28, 2016 9:03 pm
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