HP 12C Fibonacci Sequence
08-16-2017, 05:16 AM
Post: #1 Gamo Senior Member Posts: 582 Joined: Dec 2016
HP 12C Fibonacci Sequence
Here is the HP 12C program to find Fibonacci Sequence accurate up to 40
Anyone have a better solution?

Gamo

Attached File(s) FiboSequence.pdf (Size: 92.25 KB / Downloads: 29)
08-16-2017, 09:06 AM
Post: #2
 BartDB Member Posts: 160 Joined: Feb 2015
RE: HP 12C Fibonacci Sequence
(08-16-2017 05:16 AM)Gamo Wrote:  Here is the HP 12C program to find Fibonacci Sequence accurate up to 40
Anyone have a better solution?

Gamo

Hi, for interest see posts #2 & #3 in this thread in the Software Library:

Best regards

08-16-2017, 04:38 PM (This post was last modified: 08-16-2017 05:28 PM by Dieter.)
Post: #3
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: HP 12C Fibonacci Sequence
(08-16-2017 05:16 AM)Gamo Wrote:  Here is the HP 12C program to find Fibonacci Sequence accurate up to 40

Your program implements an interesting formula that calculates the Fibonacci numbers directly instead of the usual approach with adding the two previous ones. I did not know this before, but it seems to be the Moivre-Binet formula:
$F_n = \frac{1}{\sqrt{5}} \left [ \left ( \frac{1+\sqrt{5}}{2} \right )^n - \left ( \frac{1-\sqrt{5}}{2} \right )^n \right ]$
A straightforward implementation does not require any registers and could be done like this:

Code:
01 ENTER 02 5 03 √x 04 1 05 + 06 2 07 / ß8 1/x 09 x<>y 10 LstX 11 x<>y 12 y^x 13 x<>y 14 CHS 15 LstX 16 y^x 17 - 18 5 19 √x 20 / 21 INTG 22 GTO 00

(08-16-2017 05:16 AM)Gamo Wrote:  Anyone have a better solution?

Since for n≥0 the second term is always less than 1/2 it doesn't have to be calculated. Simply round $$\frac{1}{\sqrt{5}} \left (\frac{1+\sqrt{5}}{2} \right )^n$$ to the nearest integer. That's what the following program does.

Code:
01 ENTER 02 5 03 √x 04 1 05 + 06 2 07 / 08 x<>y 09 y^x 10 5 11 √x 12 / 13 . 14 3 15 + 16 INTG 17 GTO 00

On the 10-digit 12C this also works correctly up to n=40. Above that the results are a bit larger than expected because the 10-digit value of $$\Phi$$ is slightly high while $$\sqrt{5}$$ in the denominator is slightly low. That's also why the rounding function in the above code does not add 0,5 but 0,3.

BTW, using code boxes like this instead of attaching a PDF (which is only available after login) may be the better solution.

Dieter
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)