(08-09-2018 03:39 PM)Albert Chan Wrote: I removed \(0 < x < 1\) restriction of farey fraction.
Very nice! This is my attempt to translate your program for the
HP-11C:
Code:
*LBL A
STO 1 ; limit x
R↓ ; x
STO 0 ; x
INT ; ⌊x⌋
STO 2 ; lo_n
1 ; 1 lo_n
STO 3 ; lo_d=1
STO 4 ; hi_n=1
R↓ ; lo_n
RCL 0 ; x lo_n
x=y ; x = lo_n ?
GTO 4 ; return lo_n lo_d
Clx ; 0
STO 5 ; hi_d=0
*LBL 0 ; while
RCL 2 ; lo_n
RCL 4 ; hi_n lo_n
+ ; lo_n+hi_n
STO 6 ; n=lo_n+hi_n
RCL 1 ; limit
RCL 3 ; lo_d limit
RCL 5 ; hi_d lo_d limit
+ ; lo_d+hi_d limit
STO 7 ; d=lo_d+hi_d limit
x>y ; d > limit ?
GTO 3 ; break
RCL 6 ; n
RCL 0 ; x n
RCL 7 ; d x n
* ; x*d n
x≤y ; x*d ≤ n ?
GTO 1
RCL 6 ; n
STO 2 ; lo_n
RCL 7 ; d
STO 3 ; lo_d
GTO 0 ; while
*LBL 1 ; x*d n
x=y ; x*d = n ?
GTO 2 ; return n d
RCL 6 ; n
STO 4 ; hi_n
RCL 7 ; d
STO 5 ; hi_d
GTO 0 ; while
*LBL 2 ; return n d
RCL 6 ; n
RCL 7 ; d
RTN
*LBL 3 ; break
RCL 0 ; x
RCL 4 ; hi_n x
RCL 5 ; hi_d hi_n x
/ ; hi_n/hi_d x
- ; x-hi_n/hi_d
ABS ; ∆_hi=|x-hi_n/hi_d|
RCL 0 ; x ∆_hi
RCL 2 ; lo_n x ∆_hi
RCL 3 ; lo_d lo_n x ∆_hi
/ ; lo_n/lo_d x ∆_hi
- ; x-lo_n/lo_d ∆_hi
ABS ; ∆_lo=|x-lo_n/lo_d| ∆_hi
x≤y ; ∆_lo ≤ ∆_hi ?
GTO 4 ; return lo_n lo_d
RCL 4 ; hi_n
RCL 5 ; hi_d hi_n
RTN
*LBL 4
RCL 2 ; lo_n
RCL 3 ; lo_d lo_n
RTN
Quote:
Code:
>>> farey(-0.10322, 1e8) # negatives ok
(-5161, 50000)
That's not implemented yet. So I'm leaving this as an exercise for the dear reader.
Kind regards
Thomas