RosettaCodeData/Task/Continued-fraction-Arithmetic-Construct-from-rational-number/FreeBASIC/continued-fraction-arithmetic-construct-from-rational-number.basic
2023-07-01 13:44:08 -04:00

57 lines
1.1 KiB
Text

'with some other constants
data 1,2, 21,7, 21,-7, 7,21, -7,21
data 23,8, 13,11, 22,7, 3035,5258, -151,-77
data -151,77, 77,151, 77,-151, -832040,1346269
data 63018038201,44560482149, 14142,10000
data 31,10, 314,100, 3142,1000, 31428,10000, 314285,100000
data 3142857,1000000, 31428571,10000000, 314285714,100000000
data 139755218526789,44485467702853
data 534625820200,196677847971, 0,0
const Inf = -(clngint(1) shl 63)
type frc
declare sub init (byval a as longint, byval b as longint)
declare function digit () as longint
as longint n, d
end type
'member functions
sub frc.init (byval a as longint, byval b as longint)
if b < 0 then b = -b: a = -a
n = a: d = b
end sub
function frc.digit as longint
dim as longint q, r
digit = Inf
if d then
q = n \ d
r = n - q * d
'floordiv
if r < 0 then q -= 1: r += d
n = d: d = r
digit = q
end if
end function
'main
dim as longint a, b
dim r2cf as frc
do
print
read a, b
if b = 0 then exit do
r2cf.init(a, b)
do
'lazy evaluation
a = r2cf.digit
if a = Inf then exit do
print a;
loop
loop
sleep
system