HP Forums
HP 12C Fibonacci Sequence - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: General Forum (/forum-4.html)
+--- Thread: HP 12C Fibonacci Sequence (/thread-8844.html)



HP 12C Fibonacci Sequence - Gamo - 08-16-2017 05:16 AM

Here is the HP 12C program to find Fibonacci Sequence accurate up to 40
Anyone have a better solution?

Gamo


RE: HP 12C Fibonacci Sequence - BartDB - 08-16-2017 09:06 AM

(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:
http://www.hpmuseum.org/forum/thread-8352-post-73315.html?highlight=fibonacci#pid73315

Best regards


RE: HP 12C Fibonacci Sequence - Dieter - 08-16-2017 04:38 PM

(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


RE: HP 12C Fibonacci Sequence - joaomario - 02-15-2023 05:32 PM

I have come to a code to run the Fibonacci Sequence with just 13 steps!

No data need to inupted in memories or registers, just add the code and press R/S

Please, add a star to the repo if you like, that means a lot to me:

https://github.com/joaomariowd/hp-12c


RE: HP 12C Fibonacci Sequence - C.Ret - 08-08-2024 07:40 PM

Dear friends,

Like you, my heart has fallen in love with these old machines.
But my mind remains lucid.
Why so much hassle and lines of code?

Doesn't your HP-12C have, like mine I hope, a dedicated key for calculating the elements of the Fibonacci sequence? This key is marked [FV] as Fibonacci's Value.

So you just have to properly initialize your HP-12C and it can instantly calculate any value of the Fibonacci sequence with two or three keystrokes!

For example, to obtain the value of \(F_{36}\), put the display in mode in (f)[ 0 ] to display only integers.
Enter 36, press [ n ] then [ FV ] and the value of \(F_{36}\) is displayed:
[Image: attachment.php?aid=13801]

Your HP-12C can do even better and for a given element of the sequence tell you at what index it appears or at least the index of the immediate preceding element.
For example,
75025 [ FV ] [ n ] will instantly tell you that it is the 25th element!

To do this, properly initialize your HP-12C:
(f) [CLEAR FIN ]
[ 5 ] (g)[ √x ] [CHS] [1/x] [ PV ]
initializes the constant factor of the Binet Formula in the Present value register PV.
[ 5 ] (g)[ √x ] [ 1 ] [ - ] [ 5 ][ 0 ][ × ][ i ] initializes the under-exponential part of the same formula in the Interest register i%.
And there is your friendly HP-12C ready to solve all your Fibonaccial worries in the same way that it allows you to solve the financial ones.

The simplified Binet Formulae is quite a build-in expression in our beloved HP-12C:
Just observe and compare the obvious: \( FV = -PV \cdot (1 + i )^n \) versus \( F_n = \frac{1}{\sqrt{5}} \left( \frac{1+\sqrt{5}}{2} \right)^n \)


Best Regards.


RE: HP 12C Fibonacci Sequence - Werner - 08-12-2024 11:59 AM

(08-08-2024 07:40 PM)C.Ret Wrote:  To do this, properly initialize your HP-12C:
(f) [CLEAR FIN ]
[ 5 ] (g)[ √x ] [CHS] [1/x] [ PV ]
initializes the constant factor of the Binet Formula in the Present value register PV.
[ 5 ] (g)[ √x ] [ 1 ] [ - ] [ 5 ][ 0 ][ × ][ i ] initializes the under-exponential part of the same formula in the Interest register i%.
Gives wrong results as of n=40
For a few more correct results, define the constants more accurately as
.2 (g) [√x] [CHS] [PV] and
200 [ENTER] 5 [√x] 1 [+] [/] [i]
Now the first wrong result is for n=44. Mh. Doesn't gain a lot..

Cheers, Werner


RE: HP 12C Fibonacci Sequence - C.Ret - 08-12-2024 07:31 PM

(08-12-2024 11:59 AM)Werner Wrote:  For a few more correct results, define the constants more accurately as
.2 (g)[√x] [CHS] [ PV ] and
200 [ENTER] 5 [√x] 1 [ + ] [ ÷ ] [ i ]
Now the first wrong result is for n=44. Mh. Doesn't gain a lot...

Thank you for this pertinent remark.
I had not noticed that my way of doing the two calculations gave results so far from the two true values. I never think about it; it is annoying with the voyagers which only have ten digits. I am unforgivable, especially for the constant having to be raised by a power.

Now, even with all the digits correctly set, it is true that the improvement is a little disappointing and that we push back the limit of this method by only four ranks.

But, well, F_44 is already not bad.
Especially since the HP-12C can no longer give all the digits beyond F_49.

Thank you for your help.



P.S.: By re-reading my post, I just get an idea.

Since the last exact Fibonacci Value we may obtain on an HP Voyager is F_49 = 7'778'742'049.
Why not using the ability of the HP-12C to adjust the right parameters instead of trying to randomly entering accurate or inaccurate parameters manually?

The procedure may be:
_1_ Input best approximation of both parameters (using good advice to avoid unforgivable mistaken) as initial guests.
(f)[CLEAR][ FIN ]
.2 (g)[√x] [CHS] [ PV ]
200 [ENTER] 5 [√x] 1 [ + ] [ ÷ ] [ i ]

If we test result for F_49:
49 [ n ][ FV ] display erroneous value 7'778'742'061.

_2_* Try to obtain the correct value:
49 [ n ]
7778742049 [ FV ]
Now try to alternatively optimize the two parameters PV and i% to achieve expected result.

Optimize PV by pressing the [ PV ] key. The new value for PV is displayed -0.447213595
Checking the accuracy of the new configuration:
49 [ n ] [ FV ] now display 7'778'742'047. Much better but still off by two units.

Optimize i% by reentering expected value of F_49 and pressing the [ i ] key.
49 [ n ]
7778742049 [ FV ]
[ i ] now display the new adjusted i parameter 61.80339888

Checking the accuracy of the new configuration:
49 [ n ] [ FV ] still display 7'778'742'047.
No good, just continue to adjust.

Reset system:
49 [ n ]
7778742049 [ FV ]
Optimize PV again by pressing the [ PV ] key. The same value for PV is displayed -0.447213595
But same unreachable bits may have been changed internally(*), since, if we check:
49 [ n ] [ FV ] now display correct value 7'778'742'049.

But this is not the last good news :
48 [ n ] [ FV ] display 4'807'526'976.
47 [ n ] [ FV ] display 2'971'215'073.
46 [ n ] [ FV ] display 1'836'311'903.
45 [ n ] [ FV ] display 1'134'903'170.
44 [ n ] [ FV ] display 701'408'733.
...
Good settings !

(*) holding the (f)[PREFIX] key may give you some clue here about this mystery.



RE: HP 12C Fibonacci Sequence - Thomas Klemm - 08-12-2024 07:58 PM

Here's another idea:
(08-06-2023 11:06 AM)Thomas Klemm Wrote:  Splitting the Index

But since the biggest value for \(n\) is only \(49\) we can use the following formulas to reduce the index:

\(
\begin{aligned}
F_{2n-1}&={F_{n}}^{2}+{F_{n-1}}^{2}\\
F_{2n}&=(2F_{n-1}+F_{n})F_{n}\\
\end{aligned}
\)

Now even in the worst case of \(F_{49}\) we can use rounding to calculate \(F_{24}\) and \(F_{25}\).

Examples

\(
\begin{aligned}
F_{44}&=&(2F_{21}+F_{22})F_{22} &=& (2 \cdot 10,946 + 17,711) \cdot 17,711 &=& 701,408,733 \\
F_{49}&=&{F_{25}}^{2}+{F_{24}}^{2} &=& 75,025^2 + 46,386^2 &=& 7,778,742,049 \\
\end{aligned}
\)