(28/48/49/50) Convergents of Continued Fraction
12-18-2023, 07:57 PM
Post: #1
 John Keith Senior Member Posts: 1,028 Joined: Dec 2013
(28/48/49/50) Convergents of Continued Fraction
These programs converts a list of integers representing a continued fraction to a list of convergents of the continued fraction. See also the second and third basic examples here. On the 49 and 50 in exact mode, the sizes of the lists are limited only by memory. Older calculators are limited by their 12-digit integers.

The first program is for the HP 49 and 50 with the ListExt library.

Code:
 \<< DUP SIZE \-> c n   \<< c 2 LTAKE EVAL            @ Take first two terms   1 SWAP PICK3 OVER * 1 + SWAP  @ First two fractions   3. n     FOR k c k GET               @ Get next term       PICK3 OVER * 6. PICK +    @ Next numerator       PICK3 ROT * 5. PICK +     @ Next denominator     NEXT n 2. * \->LIST         @ Make list     2 LSDIV                     @ Break into 2-element sublists                 { EVAL / } LMAP             @ Make fraction from each sublist   \>> \>>

The next program is for the HP-28 and 48. It returns the convergents as 2-element sublists because the older models do not support exact rational numbers.

Code:
 \<< DUP SIZE \-> c n   \<< c 1 2 SUB LIST\-> DROP          @ No EVAL for lists   1 SWAP 3 PICK OVER * 1 + SWAP   3 n     FOR k c k GET       3 PICK OVER * 6 PICK +       3 PICK ROT * 5 PICK +     NEXT n DUP 2 * SWAP 1 +           @ From n*2 to n+1 step -1     FOR k k ROLL k ROLL 2 \->LIST -1  @ Roll top two items and make sublist     STEP n \->LIST                    @ Make list   \>> \>>

On the HP 49 and 50, this program may be used very effectively with the LongFloat library and Joe Horn's PDQ and R2CF. As an example, this short program takes a long real number (from LongFloat) and returns a continued fraction exactly representing the number.

Code:
 \<< ZZ\<-\->F NEG SWAP OVER ALOG / SWAP PDQ DROP R2CF \>>
12-18-2023, 08:25 PM
Post: #2
 Thomas Klemm Senior Member Posts: 2,082 Joined: Dec 2013
RE: (28/48/49/50) Convergents of Continued Fraction
Somewhat related: Convergents of a Continued Fraction
 « Next Oldest | Next Newest »

User(s) browsing this thread: