Convergents of a Continued Fraction
|
09-20-2024, 10:24 PM
(This post was last modified: 09-21-2024 04:17 AM by C.Ret.)
Post: #6
|
|||
|
|||
RE: Convergents of a Continued Fraction
(03-13-2022 11:41 PM)Thomas Klemm Wrote: For example, here are the convergents for \([0;1,5,2,2]\). Thank you for this very informative post and for sharing such a concise and elegant code. I also don't know how to manage four elements on the stack. However, since there are only two numerators and two denominators, we only need to use two stack elements for each new \(a_i\). The four variables come from the two numerator and denominator pairs. If we can use just one stack level for each element, we’re safe. One easy way to achieve this on an HP-15C is to use the complex-stack. I’m not sure if the same trick can be applied to other HP calculators that support complex arithmetic. Here is a simple program for the HP-15C that uses the complex stack to compute the convergents: Code: 001 { 42 21 11 } f LBL A ; Add next element a(i) to the set [ a₀ ; a₁ , a₂ , … ] Code: 011 { 42 21 15 } f LBL E ; Show Estimated value of convergent h/k Code: 016 { 42 21 13 } f LBL C ; CLEAR / redo from start LBL A or R/S: Adds the next integer \(a_i\) and compute a new convergent (h,k) where h is the real part and k the imaginary part of X: register. Also h/k is store in register R0. LBL E: Briefly displays an estimate of the current convergent (which is stored in register R0). At each step, f-(i) can be used to view the denominator; h (Re-part) and k (Im-part). The register R0 contains estimated value of convergent h/k. However, make sure to leave (h−2,k−2) and (h−1,k−1) in registers Y: and X: respectively before pressing f−A or R/S. Code: Example: [ 0 ; 1 , 5 , 2 , 2 ] Code: Example: Euler's Constant γ [ 0; 1, 1, 2, 1, 2, 1, 4, 3, 13, 5, 1, 1, 8, 1, 2, 4, 1, 1, 40, . . . I didn’t quite reach my goal, as my code is a bit longer and uses one register (I couldn’t figure out how to compute Re/Im from the stack without a register), whereas your brilliant code is shorter and only uses the stack. That said, my approach is somewhat simpler for the user, as it only requires entering the continued fraction elements in order and nothing else. |
|||
« Next Oldest | Next Newest »
|
Messages In This Thread |
Convergents of a Continued Fraction - Thomas Klemm - 03-13-2022, 11:41 PM
RE: Convergents of a Continued Fraction - Albert Chan - 09-18-2024, 04:50 PM
RE: Convergents of a Continued Fraction - Albert Chan - 09-18-2024, 08:24 PM
RE: Convergents of a Continued Fraction - Albert Chan - 09-20-2024, 12:04 AM
RE: Convergents of a Continued Fraction - Albert Chan - 09-20-2024, 05:27 PM
RE: Convergents of a Continued Fraction - C.Ret - 09-20-2024 10:24 PM
RE: Convergents of a Continued Fraction - Thomas Klemm - 09-21-2024, 08:33 AM
|
User(s) browsing this thread: 5 Guest(s)