Post Reply 
(12C) Decimal to Fraction
08-09-2018, 09:30 AM (This post was last modified: 08-09-2018 06:44 PM by Thomas Klemm.)
Post: #31
RE: (12C) Decimal to Fraction
Maybe I missed something but is there a reason for not using Farey sequences?

Python
Code:
def farey(q, n):
    a = 0
    b = c = d = 1
    while True:
        e, f = a+c, b+d
        k = f * q
        if e == k:
            return e, f
        if e < k:
            a, b = e, f
        else:
            c, d = e, f
        if b+d > n:
            return e, f

HP-11C
Code:
LBL A
STO 1       ; n q
R↓          ; q
STO 0       ; q
CLx         ; 0
STO 2       ; a = 0
1           ; 1
STO 3       ; b = 1
STO 4       ; c = 1
STO 5       ; d = 1
RCL 3       ; 1 1
+           ; 2
LBL 0       ; while
STO 7       ; f = b+d
RCL 0       ; q f
*           ; k
RCL 2       ; a k
RCL 4       ; c a k
+           ; a+c k
STO 6       ; e=a+c k
x=y         ; e = k ?
GTO 3       ; done
x≤y?        ; e < k ?
GTO 1
RCL 6       ; e
STO 4       ; c = e
RCL 7       ; f
STO 5       ; d = f
GTO 2
LBL 1
RCL 6       ; e
STO 2       ; a = e
RCL 7       ; f
STO 3       ; b = f
LBL 2
RCL 1       ; n
RCL 3       ; b n
RCL 5       ; d b n
+           ; b+d n
x≤y         ; b+d ≤ n ?
GTO 0       ; while
LBL 3       ; done
RCL 6       ; e
RCL 7       ; f
RTN

This should also be easier to convert for the HP-12C since no subroutines are used.

However the decimal number \(q\) to transform must be \(0 < q < 1\).

Example

0.10322 ENTER EEX 8 [A]
Y: 5,161
X: 50,000


Cheers
Thomas
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
(12C) Decimal to Fraction - Gamo - 08-06-2018, 12:33 PM
RE: (12C) Decimal to Fraction - Dieter - 08-06-2018, 08:44 PM
RE: (12C) Decimal to Fraction - Dieter - 08-07-2018, 07:29 AM
RE: (12C) Decimal to Fraction - Dieter - 08-07-2018, 01:02 PM
RE: (12C) Decimal to Fraction - Dieter - 08-07-2018, 11:38 AM
RE: (12C) Decimal to Fraction - Gamo - 08-07-2018, 09:29 AM
RE: (12C) Decimal to Fraction - Joe Horn - 08-07-2018, 06:28 PM
RE: (12C) Decimal to Fraction - Dieter - 08-07-2018, 07:10 PM
RE: (12C) Decimal to Fraction - Joe Horn - 08-08-2018, 12:28 AM
RE: (12C) Decimal to Fraction - Dieter - 08-08-2018, 07:04 PM
RE: (12C) Decimal to Fraction - Dieter - 08-09-2018, 09:27 AM
RE: (12C) Decimal to Fraction - Dieter - 08-08-2018, 09:44 PM
RE: (12C) Decimal to Fraction - Gamo - 08-09-2018, 12:51 AM
RE: (12C) Decimal to Fraction - Dieter - 08-09-2018, 07:46 AM
RE: (12C) Decimal to Fraction - Dieter - 08-09-2018, 08:09 AM
RE: (12C) Decimal to Fraction - Gamo - 08-09-2018, 09:16 AM
RE: (12C) Decimal to Fraction - Thomas Klemm - 08-09-2018 09:30 AM
RE: (12C) Decimal to Fraction - Dieter - 08-09-2018, 10:30 AM
RE: (12C) Decimal to Fraction - Dieter - 08-10-2018, 12:28 PM
RE: (12C) Decimal to Fraction - Gamo - 05-07-2019, 01:44 AM
RE: (12C) Decimal to Fraction - Dieter - 08-10-2018, 07:06 PM



User(s) browsing this thread: 1 Guest(s)