Continued fraction/Arithmetic/Construct from rational number: Difference between revisions

m
FreeBasic moved to the BASIC section.
m (FreeBasic moved to the BASIC section.)
Line 1,304:
314285714/100000000 => [3;7,7142857]
3142857142857143/1000000000000000 => [3;6,1,142857142857142]</pre>
 
=={{header|BASIC}}==
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">
'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
</syntaxhighlight>
{{out}}
<pre>
0 2
3
-3
0 3
-1 1 2
2 1 7
1 5 2
3 7
0 1 1 2 1 2 1 4 3 13
1 1 24 1 2
-2 25 1 2
0 1 1 24 1 2
-1 2 24 1 2
-1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 2 2 2 2 2 1 1 29
3 10
3 7 7
3 7 23 1 2
3 7 357
3 7 2857
3 7 142857
3 7 476190 3
3 7 7142857
3 7 15 1 292 1 1 1 2 1 3 1 14 2 1 1 2 2 2 2 1 84 2 1 1 15
2 1 2 1 1 4 1 1 6 1 1 8 1 1 10 1 1 12 1 1 14 1 1 16 1 1 18
</pre>
 
=={{header|C}}==
Line 2,306 ⟶ 2,397:
31428571/10000000 => [3; 7, 476190, 3]
314285714/100000000 => [3; 7, 7142857]</pre>
 
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">
'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
</syntaxhighlight>
{{out}}
<pre>
0 2
3
-3
0 3
-1 1 2
2 1 7
1 5 2
3 7
0 1 1 2 1 2 1 4 3 13
1 1 24 1 2
-2 25 1 2
0 1 1 24 1 2
-1 2 24 1 2
-1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 2 2 2 2 2 1 1 29
3 10
3 7 7
3 7 23 1 2
3 7 357
3 7 2857
3 7 142857
3 7 476190 3
3 7 7142857
3 7 15 1 292 1 1 1 2 1 3 1 14 2 1 1 2 2 2 2 1 84 2 1 1 15
2 1 2 1 1 4 1 1 6 1 1 8 1 1 10 1 1 12 1 1 14 1 1 16 1 1 18
</pre>
 
=={{header|Go}}==
511

edits