(15C) Fibonacci Numbers
|
03-23-2017, 03:22 AM
(This post was last modified: 03-23-2017 01:09 PM by Eddie W. Shore.)
Post: #1
|
|||
|
|||
(15C) Fibonacci Numbers
This program calculates the nth Fibonacci number. Store n in memory register 0 before running the program.
Formula: ( (1 + √5)^n – (1 - √5)^n ) / (2^n * √5) Caution: Due to internal calculator calculation, you may not get an integer answer. You might have to round the answer manually. From Joe Horn, with thanks and appreciation for letting me post his comment: "Hi, Eddie! I just keyed up your HP-15C program for Fibonacci numbers, and noticed that it gets wrong answers due to round-off errors much of the time. For example, with an input of 5, it should output 5, but it outputs 4.9999999998. Even rounding to the nearest integer doesn't fix the problem for inputs of 40 through 49. The only way to get the right answers for all inputs is with a loop (the usual method)." - Joe Horn I would recommend running this program when n< 40. Code:
Example: R0 = n = 16, Output: 987 |
|||
03-23-2017, 01:32 PM
Post: #2
|
|||
|
|||
RE: (15C) Fibonacci Numbers (bug with 15C LE?)
Loop Method – Joe Horn
Joe Horn provided a more accurate program which uses loops. It is slower, however should speed should not be a problem if a HP 15C Limited Edition or emulator is used. Full credit and thanks goes to Joe Horn for providing this program. Code:
A nice part is that you don’t have to pre-store n in memory 0. This method is accurate for n ≤ 49. Comparing Results Here are some results of some selected n: Code:
Bug? I manually calculated the approximation formula on my HP 15C LE (Limited Edition) and HP Prime for n = 44 and n = 49. n = 44 HP15C LE: 701408728.7 HP Prime: 701408733.002 n = 49 HP 15C LE: 7778741992 HP Prime: 7778742049.02 I think we may have found a bug on the HP 15C LE. |
|||
08-06-2023, 11:06 AM
(This post was last modified: 08-06-2023 11:56 AM by Thomas Klemm.)
Post: #3
|
|||
|
|||
RE: (15C) Fibonacci Numbers
Computation by rounding
We can use rounding to the nearest integer: \( F_{n}=\left\lfloor {\frac {\varphi ^{n}}{\sqrt {5}}}\right\rceil ,\ n\geq 0. \) However we experience errors when \(n \gt 40\). 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} \) Program for the HP-15C Code: 001 { 42 21 11 } f LBL A Timing For \(F_{49}\) it takes about 5 seconds while Joe's program takes about 30 seconds. This was measured using the Nonpareil emulator on a MacBook. But I assume that the timing will be similar on the real calculator. References |
|||
08-06-2023, 03:16 PM
Post: #4
|
|||
|
|||
RE: (15C) Fibonacci Numbers
Another idea is to calculate:
\( \left[ \begin{matrix} 0 & 1\\ 1 & 1 \end{matrix} \right]^n \) Here's a Python program that uses exponentiation by squaring: Code: from sympy import Matrix For \(F_{49}\) we can run fib(49) to get: \( \left[ \begin{matrix} 4807526976 & 7778742049\\ 7778742049 & 12586269025 \end{matrix} \right] \) Apparently we have to pick \(7778742049\). This program for the HP-15C uses matrix operations: Code: 001 { 42 21 11 } f LBL A Example It takes about 21 seconds to calculate \(F_{49}\). 49 A 7,778,742,049. |
|||
08-06-2023, 04:49 PM
Post: #5
|
|||
|
|||
RE: (15C) Fibonacci Numbers
(08-06-2023 11:06 AM)Thomas Klemm Wrote: ,,, For \(F_{49}\) it takes about 5 seconds while Joe's program takes about 30 seconds. Brilliant solution! On the 15C CE, your program returns \(F_{49}\) immediately. <0|ɸ|0> -Joe- |
|||
08-07-2023, 10:09 PM
Post: #6
|
|||
|
|||
RE: (15C) Fibonacci Numbers
(03-23-2017 01:32 PM)Eddie W. Shore Wrote: n = 44 √5 ≈ 2.23606797749979 Round to 10 digits, we have huge ulp error, almost 1/2 ULP (1+√5) relative error = 0.49979E-9 / 3.23607 = 1.54E-10 (1 + ε)^n ≈ 1 + n*ε F(44) corrections ≈ 44 * 1.54E-10 * 701408728.7 ≈ 4.8 F(44) ≈ 701408728.7 + 4.8 = 701408733.5 (error = -0.5) F(49) corrections ≈ 49 * 1.54E-10 * 7778741992 ≈ 59 F(49) ≈ 7778741992 + 59 = 7778742051 (error = -2) √20 is better, ulp error = 2 * 0.49979 - 1 = -0.00042 ULP F(n) ≈ (2+√20)^n / (2^(2n-1)*√20) On my HP12C: F(44) = 701,408,733.1 (error -0.1) F(49) = 7,778,742,050 (error -1) |
|||
08-07-2023, 10:49 PM
(This post was last modified: 08-07-2023 11:02 PM by Thomas Klemm.)
Post: #7
|
|||
|
|||
RE: (15C) Fibonacci Numbers
That's nice!
We can use your formula to calculate \(F_{47}\) exactly by rounding. This allows us to calculate: \( F_{93}=F_{47}^2+F_{46}^2 \) Edit: Bummer, the HP-16C has no \(y^x\) or similar function. |
|||
08-08-2023, 11:05 AM
(This post was last modified: 08-08-2023 12:08 PM by Werner.)
Post: #8
|
|||
|
|||
RE: (15C) Fibonacci Numbers
(08-07-2023 10:09 PM)Albert Chan Wrote: F(n) ≈ (2+√20)^n / (2^(2n-1)*√20) Hello Albert! This overflows already for n=123. perhaps better F(n) ≈ 2*(0.2+√0.2)^n / (0.4^n*√20) which can also very easily be turned into a program, eg 41-style (I don't have a 15C.. should arrive any minute now ;-) can handle up till n=248, correct for n=0..44 Update: snipped older version, see two posts down for new Cheers, Werner 41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE |
|||
08-08-2023, 11:44 AM
(This post was last modified: 08-08-2023 12:45 PM by Albert Chan.)
Post: #9
|
|||
|
|||
RE: (15C) Fibonacci Numbers | |||
08-08-2023, 12:06 PM
(This post was last modified: 08-08-2023 01:19 PM by Werner.)
Post: #10
|
|||
|
|||
RE: (15C) Fibonacci Numbers
Yes, I realized that too. The program now becomes (with 15C instructions only, I think - UPS is late):
(Update: now correct up to n=47. N=48 is off by 1, and N=49 is correct, too) 29 Bytes: Code: 01 LBL"FIB" Cheers, Werner 41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 5 Guest(s)