(15C) Fibonacci Numbers - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Software Libraries (/forum-10.html) +--- Forum: General Software Library (/forum-13.html) +--- Thread: (15C) Fibonacci Numbers (/thread-8003.html) |
(15C) Fibonacci Numbers - Eddie W. Shore - 03-23-2017 03:22 AM 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 RE: (15C) Fibonacci Numbers (bug with 15C LE?) - Eddie W. Shore - 03-23-2017 01:32 PM 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. RE: (15C) Fibonacci Numbers - Thomas Klemm - 08-06-2023 11:06 AM 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 RE: (15C) Fibonacci Numbers - Thomas Klemm - 08-06-2023 03:16 PM 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. RE: (15C) Fibonacci Numbers - Joe Horn - 08-06-2023 04:49 PM (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. RE: (15C) Fibonacci Numbers - Albert Chan - 08-07-2023 10:09 PM (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) RE: (15C) Fibonacci Numbers - Thomas Klemm - 08-07-2023 10:49 PM 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. RE: (15C) Fibonacci Numbers - Werner - 08-08-2023 11:05 AM (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 RE: (15C) Fibonacci Numbers - Albert Chan - 08-08-2023 11:44 AM (08-08-2023 11:05 AM)Werner Wrote: perhaps better We might as well combine the constant, with *same* relative error as √20 2 / √20 = √20 / 10 F(n) ≈ (0.2+√0.2)^n / 0.4^n * √0.2 Scale √0.2 last, within n = 0 .. 49, rounded, only F(48) was off by 1 RE: (15C) Fibonacci Numbers - Werner - 08-08-2023 12:06 PM 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 |