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

m
m (Added Sidef)
 
(94 intermediate revisions by 27 users not shown)
Line 33:
 
Observe how this rational number behaves differently to <math>\sqrt 2</math> and convince yourself that, in the same way as <math>3.7</math> may be represented as <math>3.70</math> when an extra decimal place is required, <math>[3;7]</math> may be represented as <math>[3;7,\infty]</math> when an extra term is required.
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F r2cf(=n1, =n2)
[Int] r
L n2 != 0
(n1, V t1_n2) = (n2, divmod(n1, n2))
n2 = t1_n2[1]
r [+]= t1_n2[0]
R r
 
print(r2cf(1, 2))
print(r2cf(3, 1))
print(r2cf(23, 8))
print(r2cf(13, 11))
print(r2cf(22, 7))
print(r2cf(14142, 10000))
print(r2cf(141421, 100000))
print(r2cf(1414214, 1000000))
print(r2cf(14142136, 10000000))</syntaxhighlight>
 
{{out}}
<pre>
[0, 2]
[3]
[2, 1, 7]
[1, 5, 2]
[3, 7]
[1, 2, 2, 2, 2, 2, 1, 1, 29]
[1, 2, 2, 2, 2, 2, 2, 3, 1, 1, 3, 1, 7, 2]
[1, 2, 2, 2, 2, 2, 2, 2, 3, 6, 1, 2, 1, 12]
[1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 1, 2, 4, 1, 1, 2]
</pre>
 
=={{header|Ada}}==
{{trans|Fortran}}
<syntaxhighlight lang="ada">
with ada.text_io; use ada.text_io;
with ada.strings; use ada.strings;
with ada.strings.fixed; use ada.strings.fixed;
 
procedure continued_fraction_from_rational is
 
-- The following implementation of r2cf both modifies its arguments
-- and returns a value.
function r2cf (N1 : in out integer;
N2 : in out integer)
return integer
is
q, r : integer;
begin
-- We will use floor division, where the quotient is rounded
-- towards negative infinity. Whenever the divisor is positive,
-- this type of division gives a non-negative remainder.
r := N1 mod N2;
q := (N1 - r) / N2;
 
N1 := N2;
N2 := r;
 
return q;
end r2cf;
 
procedure write_r2cf (N1 : in integer;
N2 : in integer)
is
M1, M2 : integer;
digit : integer;
sep : integer;
begin
put (trim (integer'image (N1), left));
put ("/");
put (trim (integer'image (N2), left));
put (" => ");
 
M1 := N1;
M2 := N2;
sep := 0;
while M2 /= 0 loop
digit := r2cf (M1, M2);
if sep = 0 then
put ("[");
sep := 1;
elsif sep = 1 then
put ("; ");
sep := 2;
else
put (", ");
end if;
put (trim (integer'image (digit), left));
end loop;
put_line ("]");
end write_r2cf;
 
begin
 
write_r2cf (1, 2);
write_r2cf (3, 1);
write_r2cf (23, 8);
write_r2cf (13, 11);
write_r2cf (22, 7);
write_r2cf (-151, 77);
 
write_r2cf (14142, 10000);
write_r2cf (141421, 100000);
write_r2cf (1414214, 1000000);
write_r2cf (14142136, 10000000);
 
write_r2cf (31, 10);
write_r2cf (314, 100);
write_r2cf (3142, 1000);
write_r2cf (31428, 10000);
write_r2cf (314285, 100000);
write_r2cf (3142857, 1000000);
write_r2cf (31428571, 10000000);
write_r2cf (314285714, 100000000);
 
end continued_fraction_from_rational;
</syntaxhighlight>
 
{{out}}
 
<pre>$ gnatmake -q continued_fraction_from_rational.adb && ./continued_fraction_from_rational
1/2 => [0; 2]
3/1 => [3]
23/8 => [2; 1, 7]
13/11 => [1; 5, 2]
22/7 => [3; 7]
-151/77 => [-2; 25, 1, 2]
14142/10000 => [1; 2, 2, 2, 2, 2, 1, 1, 29]
141421/100000 => [1; 2, 2, 2, 2, 2, 2, 3, 1, 1, 3, 1, 7, 2]
1414214/1000000 => [1; 2, 2, 2, 2, 2, 2, 2, 3, 6, 1, 2, 1, 12]
14142136/10000000 => [1; 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 1, 2, 4, 1, 1, 2]
31/10 => [3; 10]
314/100 => [3; 7, 7]
3142/1000 => [3; 7, 23, 1, 2]
31428/10000 => [3; 7, 357]
314285/100000 => [3; 7, 2857]
3142857/1000000 => [3; 7, 142857]
31428571/10000000 => [3; 7, 476190, 3]
314285714/100000000 => [3; 7, 7142857]</pre>
 
=={{header|ALGOL 68}}==
{{Trans|C}}
...with code from the Arithmetic/Rational task.<br>
The continued fraction expansion of -151/77 is sensitive to whether the language modulo operator follows the mathematical definition or the C definition.<br>
Algol 68's MOD operator uses the mathematical definition (rounds towards -infinity), so the results for -157//77 agree with the EDSAC, J and a few other sdamples. Most other samples calculate the remainder using the C definotion.
<syntaxhighlight lang="algol68">BEGIN # construct continued fraction representations of rational numbers #
# Translated from the C sample #
# Uses code from the Arithmetic/Rational task #
 
# Code from the Arithmetic/Rational task #
# ============================================================== #
 
MODE FRAC = STRUCT( INT num #erator#, den #ominator#);
 
PROC gcd = (INT a, b) INT: # greatest common divisor #
(a = 0 | b |: b = 0 | a |: ABS a > ABS b | gcd(b, a MOD b) | gcd(a, b MOD a));
PROC lcm = (INT a, b)INT: # least common multiple #
a OVER gcd(a, b) * b;
PRIO // = 9; # higher then the ** operator #
OP // = (INT num, den)FRAC: ( # initialise and normalise #
INT common = gcd(num, den);
IF den < 0 THEN
( -num OVER common, -den OVER common)
ELSE
( num OVER common, den OVER common)
FI
);
OP + = (FRAC a, b)FRAC: (
INT common = lcm(den OF a, den OF b);
FRAC result := ( common OVER den OF a * num OF a + common OVER den OF b * num OF b, common );
num OF result//den OF result
);
OP - = (FRAC a, b)FRAC: a + -b,
* = (FRAC a, b)FRAC: (
INT num = num OF a * num OF b,
den = den OF a * den OF b;
INT common = gcd(num, den);
(num OVER common) // (den OVER common)
);
OP - = (FRAC frac)FRAC: (-num OF frac, den OF frac);
# ============================================================== #
# end code from the Arithmetic/Rational task #
 
[]FRAC examples = ( 1//2, 3//1, 23//8, 13//11, 22//7, -151//77 );
[]FRAC sqrt2 = ( 14142//10000, 141421//100000, 1414214//1000000, 14142136//10000000 );
[]FRAC pi = ( 31//10, 314//100, 3142//1000, 31428//10000
, 314285//100000, 3142857//1000000, 31428571//10000000, 314285714//100000000
);
# returns the quotient of numerator over denominator and sets #
# numerator and denominator to the next values for #
# the continued fraction #
PROC r2cf = ( REF INT numerator, REF INT denominator )INT:
IF denominator = 0
THEN 0
ELSE INT quotient := numerator OVER denominator;
INT prev numerator = numerator;
numerator := denominator;
denominator := prev numerator MOD denominator;
quotient
FI # r2cf # ;
# shows the continued fractions for the elements of f seq #
PROC show r2cf = ( STRING legend, []FRAC f seq )VOID:
BEGIN
print( ( legend ) );
FOR i FROM LWB f seq TO UPB f seq DO
INT num := num OF f seq[ i ];
INT den := den OF f seq[ i ];
print( ( newline, "For N = ", whole( num , 0 ), ", D = ", whole( den , 0 ), " :" ) );
WHILE den /= 0 DO
print( ( " ", whole( r2cf( num, den ), 0 ) ) )
OD
OD
END # show r2cf # ;
BEGIN # task #
show r2cf( "Running the examples :", examples );
print( ( newline, newline ) );
show r2cf( "Running for root2 :", sqrt2 );
print( ( newline, newline ) );
show r2cf( "Running for pi :", pi )
END
END</syntaxhighlight>
{{out}}
<pre>
Running the examples :
For N = 1, D = 2 : 0 2
For N = 3, D = 1 : 3
For N = 23, D = 8 : 2 1 7
For N = 13, D = 11 : 1 5 2
For N = 22, D = 7 : 3 7
For N = -151, D = 77 : -1 25 1 2
 
Running for root2 :
For N = 7071, D = 5000 : 1 2 2 2 2 2 1 1 29
For N = 141421, D = 100000 : 1 2 2 2 2 2 2 3 1 1 3 1 7 2
For N = 707107, D = 500000 : 1 2 2 2 2 2 2 2 3 6 1 2 1 12
For N = 1767767, D = 1250000 : 1 2 2 2 2 2 2 2 2 2 6 1 2 4 1 1 2
 
Running for pi :
For N = 31, D = 10 : 3 10
For N = 157, D = 50 : 3 7 7
For N = 1571, D = 500 : 3 7 23 1 2
For N = 7857, D = 2500 : 3 7 357
For N = 62857, D = 20000 : 3 7 2857
For N = 3142857, D = 1000000 : 3 7 142857
For N = 31428571, D = 10000000 : 3 7 476190 3
For N = 157142857, D = 50000000 : 3 7 7142857
</pre>
 
=={{header|ATS}}==
 
===Using a non-linear closure===
 
In the first example solution, I demonstrate concretely that the method of integer division matters. I use 'Euclidean division' (see ACM Transactions on Programming Languages and Systems, Volume 14, Issue 2, pp 127–144. https://doi.org/10.1145/128861.128862) and show that you get a different continued fraction if you start with (-151)/77 than if you start with 151/(-77). I verified that both continued fractions do equal -(151/77).
 
A closure is used to generate the next term (or '''None()''') each time it is called. The integer type is mostly arbitrary.
 
<syntaxhighlight lang="ats">
(*------------------------------------------------------------------*)
 
#include "share/atspre_staload.hats"
 
(*------------------------------------------------------------------*)
(* First, let us implement a proper method of division of signed
integers, in which the remainder is always non-negative. (I
implement such division in my ats2-xprelude package at
https://sourceforge.net/p/chemoelectric/ats2-xprelude and have
copied the implementation from there.) *)
 
extern fn {tk : tkind}
g0int_eucliddivrem :
(g0int tk, g0int tk) -<> @(g0int tk, g0int tk)
 
implement {tk}
g0int_eucliddivrem (n, d) =
let
(* The C optimizer most likely will reduce these these two
divisions to just one. They are simply synonyms for C '/' and
'%', and perform division that rounds the quotient towards
zero. *)
val q0 = g0int_div (n, d)
val r0 = g0int_mod (n, d)
in
(* The following calculation results in 'floor division', if the
divisor is positive, or 'ceiling division', if the divisor is
negative. This choice of method results in the remainder never
being negative. *)
if isgtez n then
@(q0, r0)
else if iseqz r0 then
@(q0, r0)
else if isltz d then
@(succ q0, r0 - d)
else
@(pred q0, r0 + d)
end
 
(*------------------------------------------------------------------*)
(* I implement the lazy evaluation by having r2cf explicitly create
a thunk that returns the digits. *)
 
fn {tk : tkind}
step (N1 : ref (g0int tk),
N2 : ref (g0int tk))
: g0int tk =
let
val @(q, r) = g0int_eucliddivrem (!N1, !N2)
in
!N1 := !N2;
!N2 := r;
q
end
 
fn {tk : tkind}
r2cf (N1 : g0int tk,
N2 : g0int tk)
: () -<cloref1> Option (g0int tk) =
let
val N1 = ref<g0int tk> N1
and N2 = ref<g0int tk> N2
in
lam () =>
if iseqz !N2 then
None ()
else
Some (step (N1, N2))
end
 
(*------------------------------------------------------------------*)
 
fn {tk : tkind}
print_digits (f : () -<cloref1> Option (g0int tk))
: void =
let
fun
loop (sep : string)
: void =
case+ f () of
| None () => println! ("]")
| Some d =>
begin
print! sep;
fprint_val<g0int tk> (stdout_ref, d);
if sep = "[" then
loop "; "
else
loop ", "
end
in
loop "["
end
 
fn {tk : tkind}
print_continued_fraction
(ratnum : @(g0int tk, g0int tk))
: void =
let
val @(N1, N2) = ratnum
in
fprint_val<g0int tk> (stdout_ref, N1);
print! "/";
fprint_val<g0int tk> (stdout_ref, N2);
print! " => ";
print_digits (r2cf<tk> (N1, N2))
end
 
(*------------------------------------------------------------------*)
 
val test_cases =
$list (@(1LL, 2LL),
@(3LL, 1LL),
@(23LL, 8LL),
@(13LL, 11LL),
@(22LL, 7LL),
@(~151LL, 77LL), (* The numerator is negative. *)
@(151LL, ~77LL), (* The denominator is negative. *)
@(14142LL, 10000LL),
@(141421LL, 100000LL),
@(1414214LL, 1000000LL),
@(14142136LL, 10000000LL),
@(1414213562373095049LL, 1000000000000000000LL),
@(31LL, 10LL),
@(314LL, 100LL),
@(3142LL, 1000LL),
@(31428LL, 10000LL),
@(314285LL, 100000LL),
@(3142857LL, 1000000LL),
@(31428571LL, 10000000LL),
@(314285714LL, 100000000LL),
@(3142857142857143LL, 1000000000000000LL),
@(2200000000000000000LL, 700000000000000000LL),
@(2200000000000000001LL, 700000000000000000LL),
@(2200000000000000000LL, 700000000000000001LL))
 
implement
main0 () =
let
var p : List0 @(llint, llint)
in
println! ();
print! ("The continued fractions shown here are calculated by ");
println! ("'Euclidean division',");
println! ("where the remainder is always non-negative:");
println! ();
for (p := test_cases; ~list_is_nil p; p := list_tail p)
print_continued_fraction<llintknd> (list_head p);
println! ();
println! ("Note that [3; 6, 1] is equal to [3; 7].");
println! ()
end
 
(*------------------------------------------------------------------*)
</syntaxhighlight>
 
{{out}}
<pre>$ patscc -DATS_MEMALLOC_GCBDW -O2 -std=gnu2x continued-fraction-from-rational.dats -lgc && ./a.out
 
The continued fractions shown here are calculated by 'Euclidean division',
where the remainder is always non-negative:
 
1/2 => [0; 2]
3/1 => [3]
23/8 => [2; 1, 7]
13/11 => [1; 5, 2]
22/7 => [3; 7]
-151/77 => [-2; 25, 1, 2]
151/-77 => [-1; -2, 1, 23, 1, 2]
14142/10000 => [1; 2, 2, 2, 2, 2, 1, 1, 29]
141421/100000 => [1; 2, 2, 2, 2, 2, 2, 3, 1, 1, 3, 1, 7, 2]
1414214/1000000 => [1; 2, 2, 2, 2, 2, 2, 2, 3, 6, 1, 2, 1, 12]
14142136/10000000 => [1; 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 1, 2, 4, 1, 1, 2]
1414213562373095049/1000000000000000000 => [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, 1, 39, 1, 5, 1, 3, 61, 1, 1, 8, 1, 2, 1, 7, 1, 1, 6]
31/10 => [3; 10]
314/100 => [3; 7, 7]
3142/1000 => [3; 7, 23, 1, 2]
31428/10000 => [3; 7, 357]
314285/100000 => [3; 7, 2857]
3142857/1000000 => [3; 7, 142857]
31428571/10000000 => [3; 7, 476190, 3]
314285714/100000000 => [3; 7, 7142857]
3142857142857143/1000000000000000 => [3; 6, 1, 142857142857142]
2200000000000000000/700000000000000000 => [3; 7]
2200000000000000001/700000000000000000 => [3; 6, 1, 14285714285714284, 1, 6]
2200000000000000000/700000000000000001 => [3; 7, 4545454545454545, 3, 7]
 
Note that [3; 6, 1] is equal to [3; 7].
 
</pre>
 
=== Using a non-linear closure and multiple precision numbers ===
{{libheader|ats2-xprelude}}
{{libheader|GMP}}
{{libheader|GNU MPFR}}
 
For this you need the [https://sourceforge.net/p/chemoelectric/ats2-xprelude '''ats2-xprelude'''] package. I start with octuple precision (IEEE binary256) approximations to the square root of 2 and 22/7.
 
<syntaxhighlight lang="ats">
(*------------------------------------------------------------------*)
(* A version that uses the ats2-xprelude package
https://sourceforge.net/p/chemoelectric/ats2-xprelude
 
With ats2-xprelude installed, you can run the program with
something like:
 
patscc -DATS_MEMALLOC_GCBDW `pkg-config --variable=PATSCCFLAGS ats2-xprelude` \
`pkg-config --cflags ats2-xprelude` -O2 -std=gnu2x \
continued-fraction-from-rational-2.dats \
`pkg-config --libs ats2-xprelude` -lgc -lm && ./a.out
 
*)
 
#include "share/atspre_staload.hats"
#include "xprelude/HATS/xprelude.hats"
 
staload "xprelude/SATS/exrat.sats"
staload _ = "xprelude/DATS/exrat.dats"
 
staload "xprelude/SATS/mpfr.sats"
staload _ = "xprelude/DATS/mpfr.dats"
 
(*------------------------------------------------------------------*)
 
fn
step (ratnum : ref exrat,
done : ref bool)
: exrat =
let
(* Effectively we are doing the same thing as if we used integer
floor division and the denominator were kept positive. *)
val q = floor !ratnum
val diff = !ratnum - q
in
if iseqz diff then
begin
!done := true;
q
end
else
begin
!ratnum := reciprocal diff;
q
end
end
 
fn
r2cf (ratnum : exrat)
: () -<cloref1> Option exrat =
let
val ratnum = ref<exrat> ratnum
and done = ref<bool> false
in
lam () =>
if !done then
None ()
else
Some (step (ratnum, done))
end
 
(*------------------------------------------------------------------*)
 
fn
print_digits (f : () -<cloref1> Option exrat)
: void =
let
fun
loop (sep : string)
: void =
case+ f () of
| None () => println! ("]")
| Some d =>
begin
print! (sep, d);
if sep = "[" then
loop "; "
else
loop ", "
end
in
loop "["
end
 
fn {tk : tkind}
print_continued_fraction
(ratnum : exrat)
: void =
begin
print! (ratnum, " => ");
print_digits (r2cf ratnum)
end
 
(*------------------------------------------------------------------*)
 
(* The number of bits in the significand of an IEEE binary256
floating point number. *)
#define OCTUPLE_PREC 237
 
implement
main0 () =
begin
print_continued_fraction (exrat_make (1, 2));
print_continued_fraction (exrat_make (3, 1));
print_continued_fraction (exrat_make (23, 8));
print_continued_fraction (exrat_make (13, 11));
print_continued_fraction (exrat_make (22, 7));
print_continued_fraction (exrat_make (~151, 77));
let
val sqrt2 : mpfr = mpfr_SQRT2 OCTUPLE_PREC
val sqrt2 : exrat = g0f2f sqrt2
in
println! ("Octuple precision sqrt2:");
print_continued_fraction sqrt2
end;
let
val val_22_7 : mpfr =
mpfr_make ("22", OCTUPLE_PREC) / mpfr_make ("7", OCTUPLE_PREC)
val val_22_7 : exrat = g0f2f val_22_7
in
println! ("Octuple precision 22/7:");
print_continued_fraction val_22_7
end;
end
 
(*------------------------------------------------------------------*)
</syntaxhighlight>
 
{{out}}
<pre>$ patscc -DATS_MEMALLOC_GCBDW `pkg-config --variable=PATSCCFLAGS ats2-xprelude` `pkg-config --cflags ats2-xprelude` -O2 -std=gnu2x continued-fraction-from-rational-2.dats `pkg-config --libs ats2-xprelude` -lgc -lm && ./a.out
1/2 => [0; 2]
3 => [3]
23/8 => [2; 1, 7]
13/11 => [1; 5, 2]
22/7 => [3; 7]
-151/77 => [-2; 25, 1, 2]
Octuple precision sqrt2:
78084346301521422975112153571109417254931862326853978216001371002918963/55213970774324510299478046898216203619608871777363092441300193790394368 => [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, 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, 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, 2, 2, 2, 2, 2, 2, 2, 2, 3, 4, 6, 1, 38, 2, 1, 9, 3, 16, 2, 1, 10, 2, 2, 1, 1, 18, 1, 2, 1, 3, 4, 1, 1, 2, 6, 6, 4, 3, 2, 1, 2, 4, 2, 1, 1, 1, 9, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 3, 2, 4, 1, 7, 10, 2, 1, 4, 3, 40, 1, 1, 5, 1, 2, 2, 1, 1, 7, 7, 6, 7, 1, 1, 2, 2]
Octuple precision 22/7:
86764811216795659042036930840054034259385369935856288122043161670619721/27606985387162255149739023449108101809804435888681546220650096895197184 => [3; 7, 3943855055308893592819860492729728829972062269811649460092870985028169]
</pre>
 
===Using a non-linear closure and memoizing in an array===
 
This example solution was written specifically with the idea of providing a general representation of possibly infinitely long continued fractions. Terms can be obtained arbitrarily, in O(1) time, by their indices. One obtains '''Some term''' if the term is finite, or '''None()''' if the term is infinite.
 
One drawback is that, because a continued fraction is memoized, and its terms are generated as needed, the data structure may need to be updated. Therefore it must be stored in a mutable location, such as a '''var''' or '''ref'''.
 
<syntaxhighlight lang="ats">(*------------------------------------------------------------------*)
 
#include "share/atspre_staload.hats"
 
staload UN = "prelude/SATS/unsafe.sats"
 
(*------------------------------------------------------------------*)
(* Continued fractions as processes for generating terms. The terms
are memoized and are accessed by their zero-based index. The terms
are represented as any one of the signed integer types for which
there is a typekind. *)
 
abstype cf (tk : tkind) = ptr
 
typedef cf_generator (tk : tkind) =
() -<cloref1> Option (g0int tk)
 
extern fn {tk : tkind}
cf_make :
cf_generator tk -> cf tk
 
extern fn {tk : tkind}
{tki : tkind}
cf_get_at_guint :
{i : int}
(&cf tk >> _, g1uint (tki, i)) -> Option (g0int tk)
 
extern fn {tk : tkind}
{tki : tkind}
cf_get_at_gint :
{i : nat}
(&cf tk >> _, g1int (tki, i)) -> Option (g0int tk)
 
overload cf_get_at with cf_get_at_guint
overload cf_get_at with cf_get_at_gint
overload [] with cf_get_at
 
extern fn {tk : tkind}
cf2string_max_terms
(cf : &cf tk >> _,
max_terms : size_t)
: string
 
extern fn {tk : tkind}
cf2string_default_max_terms
(cf : &cf tk >> _)
: string
 
overload cf2string with cf2string_max_terms
overload cf2string with cf2string_default_max_terms
 
(* - - - - - - - - - - - - - *)
 
local
 
typedef _cf (tk : tkind, terminated : bool, m : int, n : int) =
[m <= n]
'{
terminated = bool terminated, (* No more terms? *)
m = size_t m, (* The number of terms computed so far. *)
n = size_t n, (* The size of memo storage.*)
memo = arrayref (g0int tk, n), (* Memoized terms. *)
gen = cf_generator tk (* A thunk to generate terms. *)
}
typedef _cf (tk : tkind, m : int) =
[terminated : bool]
[n : int | m <= n]
_cf (tk, terminated, m, n)
typedef _cf (tk : tkind) =
[m : int]
_cf (tk, m)
 
fn {tk : tkind}
_cf_get_more_terms
{terminated : bool}
{m : int}
{n : int}
{needed : int | m <= needed; needed <= n}
(cf : _cf (tk, terminated, m, n),
needed : size_t needed)
: [m1 : int | m <= m1; m1 <= needed]
[n1 : int | m1 <= n1]
_cf (tk, m1 < needed, m1, n1) =
let
prval () = lemma_g1uint_param (cf.m)
 
macdef memo = cf.memo
 
fun
loop {i : int | m <= i; i <= needed}
.<needed - i>.
(i : size_t i)
: [m1 : int | m <= m1; m1 <= needed]
[n1 : int | m1 <= n1]
_cf (tk, m1 < needed, m1, n1) =
if i = needed then
'{
terminated = false,
m = needed,
n = (cf.n),
memo = memo,
gen = (cf.gen)
}
else
begin
case+ (cf.gen) () of
| None () =>
'{
terminated = true,
m = i,
n = (cf.n),
memo = memo,
gen = (cf.gen)
}
| Some term =>
begin
memo[i] := term;
loop (succ i)
end
end
in
loop (cf.m)
end
 
fn {tk : tkind}
_cf_update {terminated : bool}
{m : int}
{n : int | m <= n}
{needed : int}
(cf : _cf (tk, terminated, m, n),
needed : size_t needed)
: [terminated1 : bool]
[m1 : int | m <= m1]
[n1 : int | m1 <= n1]
_cf (tk, terminated1, m1, n1) =
let
prval () = lemma_g1uint_param (cf.m)
macdef memo = cf.memo
macdef gen = cf.gen
in
if (cf.terminated) then
cf
else if needed <= (cf.m) then
cf
else if needed <= (cf.n) then
_cf_get_more_terms<tk> (cf, needed)
else
let (* Provides about 50% more room than is needed. *)
val n1 = needed + half needed
val memo1 = arrayref_make_elt (n1, g0i2i 0)
val () =
let
var i : [i : nat] size_t i
in
for (i := i2sz 0; i < (cf.m); i := succ i)
memo1[i] := memo[i]
end
val cf1 =
'{
terminated = false,
m = (cf.m),
n = n1,
memo = memo1,
gen = (cf.gen)
}
in
_cf_get_more_terms<tk> (cf1, needed)
end
end
 
in (* local *)
 
assume cf tk = _cf tk
 
implement {tk}
cf_make gen =
let
#ifndef CF_START_SIZE #then
#define CF_START_SIZE 8
#endif
in
'{
terminated = false,
m = i2sz 0,
n = i2sz CF_START_SIZE,
memo = arrayref_make_elt (i2sz CF_START_SIZE, g0i2i 0),
gen = gen
}
end
 
implement {tk} {tki}
cf_get_at_guint {i} (cf, i) =
let
prval () = lemma_g1uint_param i
val i : size_t i = g1u2u i
val cf1 = _cf_update<tk> (cf, succ i)
in
cf := cf1;
if i < (cf1.m) then
Some (arrayref_get_at<g0int tk> (cf1.memo, i))
else
None ()
end
 
implement {tk} {tki}
cf_get_at_gint (cf, i) =
cf_get_at_guint<tk><sizeknd> (cf, g1i2u i)
 
end (* local *)
 
implement {tk}
cf2string_max_terms (cf, max_terms) =
let
fun
loop (i : Size_t,
cf : &cf tk >> _,
sep : string,
accum : string)
: string =
if i = max_terms then
strptr2string (string_append (accum, ", ...]"))
else
begin
case+ cf[i] of
| None () =>
strptr2string (string_append (accum, "]"))
| Some term =>
let
val term_str = tostring_val<g0int tk> term
val accum =
strptr2string (string_append (accum, sep, term_str))
val sep = if sep = "[" then "; " else ", "
in
loop (succ i, cf, sep, accum)
end
end
in
loop (i2sz 0, cf, "[", "")
end
 
implement {tk}
cf2string_default_max_terms cf =
let
#ifndef DEFAULT_CF_MAX_TERMS #then
#define DEFAULT_CF_MAX_TERMS 20
#endif
in
cf2string_max_terms (cf, i2sz DEFAULT_CF_MAX_TERMS)
end
 
(*------------------------------------------------------------------*)
(* r2cf: transform a rational number to a continued fraction. *)
 
extern fn {tk : tkind}
r2cf :
(g0int tk, g0int tk) -> cf tk
 
(* - - - - - - - - - - - - - *)
 
implement {tk}
r2cf (n, d) =
let
val n = ref<g0int tk> n
val d = ref<g0int tk> d
 
fn
gen () :<cloref1> Option (g0int tk) =
if iseqz !d then
None ()
else
let
(* The method of rounding the quotient seems unimportant,
and so let us simply use the truncation towards zero
that is native to C. *)
val numer = !n
and denom = !d
val q = numer / denom
and r = numer mod denom
in
!n := denom;
!d := r;
Some q
end
in
cf_make gen
end
 
(*------------------------------------------------------------------*)
 
val some_rationals =
$list (@(1LL, 2LL),
@(3LL, 1LL),
@(23LL, 8LL),
@(13LL, 11LL),
@(22LL, 7LL),
@(~151LL, 77LL),
@(14142LL, 10000LL),
@(141421LL, 100000LL),
@(1414214LL, 1000000LL),
@(14142136LL, 10000000LL),
@(1414213562373095049LL, 1000000000000000000LL),
@(31LL, 10LL),
@(314LL, 100LL),
@(3142LL, 1000LL),
@(31428LL, 10000LL),
@(314285LL, 100000LL),
@(3142857LL, 1000000LL),
@(31428571LL, 10000000LL),
@(314285714LL, 100000000LL),
@(3142857142857143LL, 1000000000000000LL))
 
implement
main0 () =
let
var p : List0 @(llint, llint)
in
for (p := some_rationals; ~list_is_nil p; p := list_tail p)
let
val @(n, d) = list_head<@(llint, llint)> p
var cf = r2cf (n, d)
in
println! (n, "/", d, " => ", cf2string cf)
end
end
 
(*------------------------------------------------------------------*)
</syntaxhighlight>
 
{{out}}
 
<pre>$ patscc -DATS_MEMALLOC_GCBDW -O2 -std=gnu2x continued-fraction-from-rational-3.dats -lgc && ./a.out
1/2 => [0; 2]
3/1 => [3]
23/8 => [2; 1, 7]
13/11 => [1; 5, 2]
22/7 => [3; 7]
-151/77 => [-1; -1, -24, -1, -2]
14142/10000 => [1; 2, 2, 2, 2, 2, 1, 1, 29]
141421/100000 => [1; 2, 2, 2, 2, 2, 2, 3, 1, 1, 3, 1, 7, 2]
1414214/1000000 => [1; 2, 2, 2, 2, 2, 2, 2, 3, 6, 1, 2, 1, 12]
14142136/10000000 => [1; 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 1, 2, 4, 1, 1, 2]
1414213562373095049/1000000000000000000 => [1; 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, ...]
31/10 => [3; 10]
314/100 => [3; 7, 7]
3142/1000 => [3; 7, 23, 1, 2]
31428/10000 => [3; 7, 357]
314285/100000 => [3; 7, 2857]
3142857/1000000 => [3; 7, 142857]
31428571/10000000 => [3; 7, 476190, 3]
314285714/100000000 => [3; 7, 7142857]
3142857142857143/1000000000000000 => [3; 6, 1, 142857142857142]</pre>
 
===Using a non-linear lazy list===
{{trans|Haskell}}
 
This implementation is inspired by the Haskell, although the way in which the integer divisions are done may differ.
 
Terms are memoized but as a linked list. For sequential access of terms, this is fine.
 
<syntaxhighlight lang="ats">
(* Continued fractions as non-linear lazy lists (stream types).
 
I avoid the shorthands stream_make_nil and stream_make_cons,
so the thunk-making is visible. *)
 
#include "share/atspre_staload.hats"
 
typedef cf (tk : tkind) = stream (g0int tk)
 
extern fn {tk : tkind}
r2cf : (g0int tk, g0int tk) -> cf tk
 
extern fn {tk : tkind}
cf2string : cf tk -> string
 
implement {tk}
r2cf (n, d) =
let
fun
recurs (n : g0int tk,
d : g0int tk)
: cf tk =
if iseqz d then
$delay stream_nil ()
else
let
val q = n / d
and r = n mod d
in
$delay stream_cons (q, recurs (d, r))
end
in
recurs (n, d)
end
 
implement {tk}
cf2string cf =
let
val max_terms = 2000
 
fun
loop (i : intGte 0,
cf : cf tk,
slist : List0 string)
: List0 string =
(* One has to say "!cf" instead of just "cf", to force the lazy
evaluation. If you simply wrote "cf", typechecking would
fail. *)
case+ !cf of
| stream_nil () => list_cons ("]", slist)
| stream_cons (term, cf) =>
if i = max_terms then
list_cons (",...]", slist)
else
let
val sep_str =
case+ i of
| 0 => ""
| 1 => ";"
| _ => ","
val term_str = tostring_val<g0int tk> term
val slist = list_cons (term_str,
list_cons (sep_str, slist))
in
loop (succ i, cf, slist)
end
 
val slist = loop (0, cf, list_sing "[")
val slist = list_vt2t (reverse slist)
in
strptr2string (stringlst_concat slist)
end
 
fn {tk : tkind}
show (n : g0int tk,
d : g0int tk)
: void =
begin
print! (tostring_val<g0int tk> n);
print! "/";
print! (tostring_val<g0int tk> d);
print! " => ";
println! (cf2string<tk> (r2cf<tk> (n, d)))
end
 
implement
main () =
begin
show<intknd> (1, 2);
show<lintknd> (g0i2i 3, g0i2i 1);
show<llintknd> (g0i2i 23, g0i2i 8);
show (13, 11);
show (22L, 11L);
show (~151LL, 77LL);
show (14142LL, 10000LL);
show (141421LL, 100000LL);
show (1414214LL, 1000000LL);
show (14142136LL, 10000000LL);
show (1414213562373095049LL, 1000000000000000000LL);
show (31LL, 10LL);
show (314LL, 100LL);
show (3142LL, 1000LL);
show (31428LL, 10000LL);
show (314285LL, 100000LL);
show (3142857LL, 1000000LL);
show (31428571LL, 10000000LL);
show (314285714LL, 100000000LL);
show (3142857142857143LL, 1000000000000000LL);
0
end;
</syntaxhighlight>
 
{{out}}
<pre>$ patscc -g -O2 -std=gnu2x -DATS_MEMALLOC_GCBDW continued-fraction-from-rational-4.dats -lgc && ./a.out
1/2 => [0;2]
3/1 => [3]
23/8 => [2;1,7]
13/11 => [1;5,2]
22/11 => [2]
-151/77 => [-1;-1,-24,-1,-2]
14142/10000 => [1;2,2,2,2,2,1,1,29]
141421/100000 => [1;2,2,2,2,2,2,3,1,1,3,1,7,2]
1414214/1000000 => [1;2,2,2,2,2,2,2,3,6,1,2,1,12]
14142136/10000000 => [1;2,2,2,2,2,2,2,2,2,6,1,2,4,1,1,2]
1414213562373095049/1000000000000000000 => [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,1,39,1,5,1,3,61,1,1,8,1,2,1,7,1,1,6]
31/10 => [3;10]
314/100 => [3;7,7]
3142/1000 => [3;7,23,1,2]
31428/10000 => [3;7,357]
314285/100000 => [3;7,2857]
3142857/1000000 => [3;7,142857]
31428571/10000000 => [3;7,476190,3]
314285714/100000000 => [3;7,7142857]
3142857142857143/1000000000000000 => [3;6,1,142857142857142]</pre>
 
===Using a linear lazy list===
{{trans|Haskell}}
 
This implementation is inspired by the Haskell, although the way in which the integer divisions are done may differ.
 
<syntaxhighlight lang="ats">
(* Continued fractions as linear lazy lists (stream_vt types).
 
I use the shorthands stream_vt_make_nil and stream_vt_make_cons,
rather than explicitly write "$ldelay". To see how the shorthands
are implemented, see prelude/DATS/stream_vt.dats *)
 
#include "share/atspre_staload.hats"
 
vtypedef cf_vt (tk : tkind) = stream_vt (g0int tk)
 
extern fn {tk : tkind}
r2cf_vt : (g0int tk, g0int tk) -> cf_vt tk
 
(* Note that cf_vt2strptr CONSUMES the stream. The stream will no
longer exist.
 
If you are using linear streams, I would imagine you might want to
(for instance) convert to list_vt the parts you want to reuse. *)
extern fn {tk : tkind}
cf_vt2strptr : cf_vt tk -> Strptr1
 
implement {tk}
r2cf_vt (n, d) =
let
typedef integer = g0int tk
fun
recurs (n : integer,
d : integer)
: cf_vt tk =
if iseqz d then
stream_vt_make_nil<integer> ()
else
let
val q = n / d
and r = n mod d
in
stream_vt_make_cons<integer> (q, recurs (d, r))
end
in
recurs (n, d)
end
 
implement {tk}
cf_vt2strptr cf =
let
val max_terms = 2000
 
typedef integer = g0int tk
 
fun
loop (i : intGte 0,
cf : cf_vt tk,
slist : List0_vt Strptr1)
: List0_vt Strptr1 =
let
val cf_con = !cf (* Force evaluation. *)
in
case+ cf_con of
| ~ stream_vt_nil () => list_vt_cons (copy "]", slist)
| ~ stream_vt_cons (term, tail) =>
if i = max_terms then
let
val slist = list_vt_cons (copy ",...]", slist)
in
~ tail;
slist
end
else
let
val sep_str =
copy ((case+ i of
| 0 => ""
| 1 => ";"
| _ => ",") : string)
val term_str = tostrptr_val<g0int tk> term
val slist = list_vt_cons (term_str,
list_vt_cons (sep_str, slist))
in
loop (succ i, tail, slist)
end
end
 
val slist = loop (0, cf, list_vt_sing (copy "["))
val slist = reverse slist
val s = strptrlst_concat slist
val () = assertloc (isneqz s)
in
s
end
 
fn {tk : tkind}
show (n : g0int tk,
d : g0int tk)
: void =
let
val n_str = tostrptr_val<g0int tk> n
and d_str = tostrptr_val<g0int tk> d
and cf_str = cf_vt2strptr<tk> (r2cf_vt<tk> (n, d))
in
print! n_str;
print! "/";
print! d_str;
print! " => ";
println! cf_str;
strptr_free n_str;
strptr_free d_str;
strptr_free cf_str
end
 
implement
main () =
begin
show<intknd> (1, 2);
show<lintknd> (g0i2i 3, g0i2i 1);
show<llintknd> (g0i2i 23, g0i2i 8);
show (13, 11);
show (22L, 11L);
show (~151LL, 77LL);
show (14142LL, 10000LL);
show (141421LL, 100000LL);
show (1414214LL, 1000000LL);
show (14142136LL, 10000000LL);
show (1414213562373095049LL, 1000000000000000000LL);
show (31LL, 10LL);
show (314LL, 100LL);
show (3142LL, 1000LL);
show (31428LL, 10000LL);
show (314285LL, 100000LL);
show (3142857LL, 1000000LL);
show (31428571LL, 10000000LL);
show (314285714LL, 100000000LL);
show (3142857142857143LL, 1000000000000000LL);
0
end;
</syntaxhighlight>
 
{{out}}
<pre>$ patscc -g -O2 -std=gnu2x -DATS_MEMALLOC_LIBC continued-fraction-from-rational-5.dats && ./a.out
1/2 => [0;2]
3/1 => [3]
23/8 => [2;1,7]
13/11 => [1;5,2]
22/11 => [2]
-151/77 => [-1;-1,-24,-1,-2]
14142/10000 => [1;2,2,2,2,2,1,1,29]
141421/100000 => [1;2,2,2,2,2,2,3,1,1,3,1,7,2]
1414214/1000000 => [1;2,2,2,2,2,2,2,3,6,1,2,1,12]
14142136/10000000 => [1;2,2,2,2,2,2,2,2,2,6,1,2,4,1,1,2]
1414213562373095049/1000000000000000000 => [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,1,39,1,5,1,3,61,1,1,8,1,2,1,7,1,1,6]
31/10 => [3;10]
314/100 => [3;7,7]
3142/1000 => [3;7,23,1,2]
31428/10000 => [3;7,357]
314285/100000 => [3;7,2857]
3142857/1000000 => [3;7,142857]
31428571/10000000 => [3;7,476190,3]
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}}==
C does not implement Lazy evaluation and it is this particular feature which is the real challenge of this particular example. It can however be simulated. The following example uses pointers. It seems that the same data is being passed but since the function accepts pointers, the variables are being changed. One other way to simulate laziness would be to use global variables. Then although it would seem that the same values are being passed even as constants, the job is actually getting done. In my view, that would be plain cheating.
 
<syntaxhighlight lang="c">
<lang C>
/*Abhishek Ghosh, 8th November 2013, Rotterdam*/
 
#include<stdio.h>
 
Line 110 ⟶ 1,472:
}
</langsyntaxhighlight>
And the run gives :
<pre>
Line 137 ⟶ 1,499:
For N = 314285714, D = 100000000 : 3 7 7142857
</pre>
 
=={{header|C sharp|C#}}==
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
 
class Program
{
static IEnumerable<int> r2cf(int n1, int n2)
{
while (Math.Abs(n2) > 0)
{
int t1 = n1 / n2;
int t2 = n2;
n2 = n1 - t1 * n2;
n1 = t2;
yield return t1;
}
}
 
static void spit(IEnumerable<int> f)
{
foreach (int n in f) Console.Write(" {0}", n);
Console.WriteLine();
}
 
static void Main(string[] args)
{
spit(r2cf(1, 2));
spit(r2cf(3, 1));
spit(r2cf(23, 8));
spit(r2cf(13, 11));
spit(r2cf(22, 7));
spit(r2cf(-151, 77));
for (int scale = 10; scale <= 10000000; scale *= 10)
{
spit(r2cf((int)(Math.Sqrt(2) * scale), scale));
}
spit(r2cf(31, 10));
spit(r2cf(314, 100));
spit(r2cf(3142, 1000));
spit(r2cf(31428, 10000));
spit(r2cf(314285, 100000));
spit(r2cf(3142857, 1000000));
spit(r2cf(31428571, 10000000));
spit(r2cf(314285714, 100000000));
}
}
</syntaxhighlight>
Output
<pre>
0 2
3
2 1 7
1 5 2
3 7
-1 -1 -24 -1 -2
1 2 2
1 2 2 3 1 1 2
1 2 2 2 2 5 3
1 2 2 2 2 2 1 1 29
1 2 2 2 2 2 2 3 1 1 3 1 7 2
1 2 2 2 2 2 2 2 1 1 4 1 1 1 1 1 2 1 6
1 2 2 2 2 2 2 2 2 2 1 594
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</pre>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
/* Interface for all Continued Fractions
Nigel Galloway, February 9th., 2013.
Line 170 ⟶ 1,603:
const int nextTerm() {if (first) {first = false; return 1;} else return 2;}
const bool moreTerms() {return true;}
};</langsyntaxhighlight>
===Testing===
====1/2 3 23/8 13/11 22/7 -151/77====
<langsyntaxhighlight lang="cpp">int main() {
for(r2cf n(1,2); n.moreTerms(); std::cout << n.nextTerm() << " ");
std::cout << std::endl;
Line 187 ⟶ 1,620:
std::cout << std::endl;
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 198 ⟶ 1,631:
</pre>
====<math>\sqrt 2</math>====
<langsyntaxhighlight lang="cpp">int main() {
int i = 0;
for(SQRT2 n; i++ < 20; std::cout << n.nextTerm() << " ");
Line 207 ⟶ 1,640:
std::cout << std::endl;
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 215 ⟶ 1,648:
</pre>
====Real approximations of a rational number====
<langsyntaxhighlight lang="cpp">int main() {
for(r2cf n(31,10); n.moreTerms(); std::cout << n.nextTerm() << " ");
std::cout << std::endl;
Line 233 ⟶ 1,666:
std::cout << std::endl;
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 246 ⟶ 1,679:
</pre>
 
=={{header|C sharp|C#Clojure}}==
<syntaxhighlight lang="clojure">(defn r2cf [n d]
<lang csharp>using System;
(if-not (= d 0) (cons (quot n d) (lazy-seq (r2cf d (rem n d))))))
using System.Collections.Generic;
 
; Example usage
class Program
(def demo '((1 2)
{
(3 1)
static IEnumerable<int> r2cf(int n1, int n2)
{ (23 8)
while (Math.Abs(n2) > 0 (13 11)
{ (22 7)
int(-151 t1 = n1 / n2;77)
int(14142 t2 = n2;10000)
n2(141421 = n1 - t1 * n2;100000)
n1(1414214 = t2;1000000)
yield(14142136 return t1;10000000)
} (31 10)
(314 100)
}
(3142 1000)
(31428 10000)
(314285 100000)
(3142857 1000000)
(31428571 10000000)
(314285714 100000000)
(3141592653589793 1000000000000000)))
 
(doseq [inputs demo
static void spit(IEnumerable<int> f)
:let [outputs (r2cf (first inputs) (last inputs))]]
{
(println inputs ";" outputs))</syntaxhighlight>
foreach (int n in f) Console.Write(" {0}", n);
Console.WriteLine();
}
 
{{out}}
static void Main(string[] args)
{
spit(r2cf(1, 2));
spit(r2cf(3, 1));
spit(r2cf(23, 8));
spit(r2cf(13, 11));
spit(r2cf(22, 7));
spit(r2cf(-151, 77));
for (int scale = 10; scale <= 10000000; scale *= 10)
{
spit(r2cf((int)(Math.Sqrt(2) * scale), scale));
}
spit(r2cf(31, 10));
spit(r2cf(314, 100));
spit(r2cf(3142, 1000));
spit(r2cf(31428, 10000));
spit(r2cf(314285, 100000));
spit(r2cf(3142857, 1000000));
spit(r2cf(31428571, 10000000));
spit(r2cf(314285714, 100000000));
}
}
</lang>
Output
<pre>
(1 2) ; (0 2)
(3 1) ; (3)
3
(23 8) ; (2 1 7)
(13 11) ; (1 5 2)
(22 7) ; (3 7)
(-151 77) ; (-1 -1 -24 -1 -2)
(14142 10000) ; (1 2 2 2 2 2 1 1 29)
1 2 2
(141421 100000) ; (1 2 2 2 2 2 2 3 1 1 3 1 7 2)
(1414214 1000000) ; (1 2 2 2 2 52 2 2 3 6 1 2 1 12)
(14142136 10000000) ; (1 2 2 2 2 2 2 2 2 2 6 1 2 4 1 291 2)
(31 10) ; (3 10)
1 2 2 2 2 2 2 3 1 1 3 1 7 2
(314 100) ; (3 7 7)
1 2 2 2 2 2 2 2 1 1 4 1 1 1 1 1 2 1 6
1(3142 21000) 2; 2(3 27 223 21 2 2 2 1 594)
(31428 10000) ; (3 7 357)
3 10
(314285 100000) ; (3 7 2857)
3 7 7
(3142857 1000000) ; (3 7 142857)
3 7 23 1 2
(31428571 10000000) ; (3 7 476190 3)
3 7 357
(314285714 100000000) ; (3 7 7142857)
3 7 2857
(3141592653589793 1000000000000000) ; (3 7 15 1 292 1 1 1 2 1 3 1 14 4 2 3 1 12 5 1 5 20 1 11 1 1 1 2)
3 7 142857
</pre>
3 7 476190 3
3 7 7142857</pre>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defun r2cf (n1 n2)
(lambda ()
(unless (zerop n2)
Line 358 ⟶ 1,772:
(31428571 10000000)
(314285714 100000000)
(3141592653589793 1000000000000000)))</langsyntaxhighlight>
 
Output:
Line 381 ⟶ 1,795:
(R2CF 3141592653589793 1000000000000000) ; (3 7 15 1 292 1 1 1 2 1 3 1 14 4 2 3 1 12 5 1 5 20 1 11 1 1 1 2)
</pre>
 
=={{header|D}}==
{{trans|Kotlin}}
<syntaxhighlight lang="d">import std.concurrency;
import std.stdio;
 
struct Pair {
int first, second;
}
 
auto r2cf(Pair frac) {
return new Generator!int({
auto num = frac.first;
auto den = frac.second;
while (den != 0) {
auto div = num / den;
auto rem = num % den;
num = den;
den = rem;
div.yield();
}
});
}
 
void iterate(Generator!int seq) {
foreach(i; seq) {
write(i, " ");
}
writeln();
}
 
void main() {
auto fracs = [
Pair( 1, 2),
Pair( 3, 1),
Pair( 23, 8),
Pair( 13, 11),
Pair( 22, 7),
Pair(-151, 77),
];
foreach(frac; fracs) {
writef("%4d / %-2d = ", frac.first, frac.second);
frac.r2cf.iterate;
}
writeln;
 
auto root2 = [
Pair( 14_142, 10_000),
Pair( 141_421, 100_000),
Pair( 1_414_214, 1_000_000),
Pair(14_142_136, 10_000_000),
];
writeln("Sqrt(2) ->");
foreach(frac; root2) {
writef("%8d / %-8d = ", frac.first, frac.second);
frac.r2cf.iterate;
}
writeln;
 
auto pi = [
Pair( 31, 10),
Pair( 314, 100),
Pair( 3_142, 1_000),
Pair( 31_428, 10_000),
Pair( 314_285, 100_000),
Pair( 3_142_857, 1_000_000),
Pair( 31_428_571, 10_000_000),
Pair(314_285_714, 100_000_000),
];
writeln("Pi ->");
foreach(frac; pi) {
writef("%9d / %-9d = ", frac.first, frac.second);
frac.r2cf.iterate;
}
}</syntaxhighlight>
 
{{out}}
<pre> 1 / 2 = 0 2
3 / 1 = 3
23 / 8 = 2 1 7
13 / 11 = 1 5 2
22 / 7 = 3 7
-151 / 77 = -1 -1 -24 -1 -2
 
Sqrt(2) ->
14142 / 10000 = 1 2 2 2 2 2 1 1 29
141421 / 100000 = 1 2 2 2 2 2 2 3 1 1 3 1 7 2
1414214 / 1000000 = 1 2 2 2 2 2 2 2 3 6 1 2 1 12
14142136 / 10000000 = 1 2 2 2 2 2 2 2 2 2 6 1 2 4 1 1 2
 
Pi ->
31 / 10 = 3 10
314 / 100 = 3 7 7
3142 / 1000 = 3 7 23 1 2
31428 / 10000 = 3 7 357
314285 / 100000 = 3 7 2857
3142857 / 1000000 = 3 7 142857
31428571 / 10000000 = 3 7 476190 3
314285714 / 100000000 = 3 7 7142857</pre>
 
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
function GetNextFraction(var N1,N2: integer): integer;
{Get next step in fraction series}
var R: integer;
begin
R:=FloorMod(N1, N2);
Result:= FloorDiv(N1 - R , N2);
N1 := N2;
N2 := R;
end;
 
procedure GetFractionList(N1,N2: integer; var IA: TIntegerDynArray);
{Get list of continuous fraction values}
var M1, M2,F : integer;
var S: integer;
begin
SetLength(IA,0);
M1 := N1;
M2 := N2;
while M2<>0 do
begin
F:=GetNextFraction(M1,M2);
SetLength(IA,Length(IA)+1);
IA[High(IA)]:=F;
end;
end;
 
 
procedure ShowConFraction(Memo: TMemo; N1,N2: integer);
{Calculate and display continues fraction for Rational number N1/N2}
var I: integer;
var S: string;
var IA: TIntegerDynArray;
begin
GetFractionList(N1,N2,IA);
S:='[';
for I:=0 to High(IA) do
begin
if I>0 then S:=S+', ';
S:=S+IntToStr(IA[I]);
end;
S:=S+']';
Memo.Lines.Add(Format('%10d / %10d --> ',[N1,N2])+S);
end;
 
 
procedure ShowContinuedFractions(Memo: TMemo);
{Show all continued fraction tests}
begin
Memo.Lines.Add('Wikipedia Example');
ShowConFraction(Memo,415,93);
 
Memo.Lines.Add('');
Memo.Lines.Add('Rosetta Code Examples');
ShowConFraction(Memo,1, 2);
ShowConFraction(Memo,3, 1);
ShowConFraction(Memo,23, 8);
ShowConFraction(Memo,13, 11);
ShowConFraction(Memo,22, 7);
ShowConFraction(Memo,-151, 77);
 
 
Memo.Lines.Add('');
Memo.Lines.Add('Square Root of Two');
ShowConFraction(Memo,14142, 10000);
ShowConFraction(Memo,141421, 100000);
ShowConFraction(Memo,1414214, 1000000);
ShowConFraction(Memo,14142136, 10000000);
 
Memo.Lines.Add('');
Memo.Lines.Add('PI');
ShowConFraction(Memo,31, 10);
ShowConFraction(Memo,314, 100);
ShowConFraction(Memo,3142, 1000);
ShowConFraction(Memo,31428, 10000);
ShowConFraction(Memo,314285, 100000);
ShowConFraction(Memo,3142857, 1000000);
ShowConFraction(Memo,31428571, 10000000);
ShowConFraction(Memo,314285714, 100000000);
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
Wikipedia Example
415 / 93 --> [4, 2, 6, 7]
 
Rosetta Code Examples
1 / 2 --> [0, 2]
3 / 1 --> [3]
23 / 8 --> [2, 1, 7]
13 / 11 --> [1, 5, 2]
22 / 7 --> [3, 7]
-151 / 77 --> [-2, 25, 1, 2]
 
Square Root of Two
14142 / 10000 --> [1, 2, 2, 2, 2, 2, 1, 1, 29]
141421 / 100000 --> [1, 2, 2, 2, 2, 2, 2, 3, 1, 1, 3, 1, 7, 2]
1414214 / 1000000 --> [1, 2, 2, 2, 2, 2, 2, 2, 3, 6, 1, 2, 1, 12]
14142136 / 10000000 --> [1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 1, 2, 4, 1, 1, 2]
 
PI
31 / 10 --> [3, 10]
314 / 100 --> [3, 7, 7]
3142 / 1000 --> [3, 7, 23, 1, 2]
31428 / 10000 --> [3, 7, 357]
314285 / 100000 --> [3, 7, 2857]
3142857 / 1000000 --> [3, 7, 142857]
31428571 / 10000000 --> [3, 7, 476190, 3]
314285714 / 100000000 --> [3, 7, 7142857]
Elapsed Time: 41.009 ms.
 
</pre>
 
 
=={{header|EDSAC order code}}==
Besides the assigned task, this program demonstrates a division subroutine
for 35-bit positive integers, returning quotient and remainder.
<syntaxhighlight lang="edsac">
[Continued fractions from rationals.
EDSAC program, Initial Orders 2.]
 
[Memory usage:
56..109 Print subroutine, modified from the EDSAC library
110..146 Division subroutine for long positive integers
148..196 Continued fraction subroutine, as specified by Rosetta Code
200..260 Main routine
262.. List of rationals, variable number of items]
 
[Define where to store the list of rationals.]
T 45 K [store address in location 45;
values are then accessed by code letter H (*)]
P 262 F [<------ address here]
[(*) Arbitrary choice. We could equally well use 46 and N, 47 and M, etc.]
 
[Library subroutine R2. Reads positive integers during input of orders,
and is then overwritten (so doesn't take up any memory).
Negative numbers can be input by adding 2^35.
Each integer is followed by 'F', except the last is followed by '#TZ'.]
GKT20FVDL8FA40DUDTFI40FA40FS39FG@S2FG23FA5@T5@E4@E13Z
T #H [Tell R2 the storage location defined above]
 
[Rationals to be read by R2. First item is count, then num/den pairs.]
8F 1F2F 3F1F 33F8F 13F11F 22F7F 34359738217F77F
141421356F100000000F 314285714F100000000#TZ
 
[----------------------------------------------------------------------
Modification of library subroutine P7.
Prints signed integer up to 10 digits, left-justified.
54 storage locations; working position 4D.
Must be loaded at an even address.
Input: Number is at 0D.]
T 56 K
GKA3FT42@A49@T31@ADE10@T31@A48@T31@SDTDH44#@NDYFLDT4DS43@TF
H17@S17@A43@G23@UFS43@T1FV4DAFG50@SFLDUFXFOFFFSFL4FT4DA49@
T31@A1FA43@G20@XFP1024FP610D@524D!FO46@O26@XFSFL8FT4DE39@
 
[----------------------------------------------------------------------
Division subroutine for long positive integers.
35-bit dividend and divisor (max 2^34 - 1)
returning quotient and remainder.
Input: dividend at 4D, divisor at 6D
Output: remainder at 4D, quotient at 6D.
Working locations 0D, 8D.]
T 110 K
G K
A 3 F [plant link]
T 35 @
A 6 D [load divisor]
U 8 D [save at 8D (6D is required for quotient)]
[4] T D [initialize shifted divisor]
A 4 D [load dividend]
R D [shift 1 right]
S D [shifted divisor > dividend/2 yet?]
G 13 @ [yes, start subtraction]
T 36 @ [no, clear acc]
A D [shift divisor 1 more]
L D
E 4 @ [loop back (always, since acc >= 0)]
[13] T 36 @ [clear acc]
T 6 D [initialize quotient to 0]
[15] A 4 D [load remainder (initially = dividend)]
S D [trial subtraction]
G 23 @ [skip if can't subtract]
T 4 D [update remainder]
A 6 D [load quotient]
Y F [add 1 by rounding twice (*)]
Y F
T 6 D
[23] T 36 @ [clear acc]
A 8 D [load original divisor]
S D [is shifted divisor back to original?]
E 35 @ [yes, exit (with accumulator = 0,
in accordance with EDSAC convention)]
T 36 @ [no, clear acc]
A D [shift divisor 1 right]
R D
T D
A 6 D [shift quotient 1 left]
L D
T 6 D
E 15 @ [loop back (always, since acc = 0)]
[35] E F [return; order set up at runtime]
[36] P F [junk word, to clear accumulator]
 
[(*) This saves the bother of defining a double-word constant 1
and making sure that it's at an even address.]
 
[----------------------------------------------------------------------
Subroutine for lazy evaluation of continued fraction.
Must be loaded at an even address.
Locations relative to start of subroutine:
0: Entry point
1: Flag, < 0 if c.f. is finished, >= 0 if there's another term
2, 3: Next term of c.f., provided the flag (location 1) is >= 0
4, 5: Caller places numerator here before first call
6, 7: Caller places denominator here before first call; must be > 0
 
After setting up the numerator and denominator of the rational number,
the caller should repeatedly call location 0, reading the result
from location 1 and double location 2.
Locations 4..7 are maintained by the subroutine and should not be changed
by the caller until a new continued fraction is required.]
 
T 46 K [place address of subroutine in location 46]
P 148 F
E 25 K [load the code below to that address (WWG page 18)]
T N
G K
[0] G 8 @ [entry point]
[1] P F [flag returned here]
[2] P F P F [term returned here, if flag >= 0;
also used as temporary store]
[4] P F P F [caller puts numerator here]
[6] P F P F [caller puts denominator here]
[8] A 3 F [plant link]
T 28 @
S 6#@ [load negative of denominator]
E 44 @ [if denom <= 0, no more terms]
T F [clear acc]
A 4#@ [load numerator]
T 2#@ [save before overwriting]
A 6#@ [load denominator]
U 4#@ [make it numerator for next call]
T 6 D [also to 6D for division]
A 2#@ [load numerator]
G 29 @ [special action if negative]
T 4 D [to 4D for division]
[21] A 21 @ [for return from next]
G 110 F [call the above division subroutine]
A 4 D [load remainder]
T 6#@ [make it denominator for next call]
A 6 D [load quotient]
[26] T 2#@ [return it as next term]
[27] T 1 @ [flag >= 0 means term is valid]
[28] E F [exit with acc = 0]
 
[Here if rational = -n/d where n, d > 0. Principle is:
if n + d - 1 = qd + r then -n = -qd + (d - 1 - r)]
[29] T 4 D [save numerator in 4D]
S 6 D [acc := -den]
Y F [add 1 by rounding twice]
Y F
T 2#@ [save (1 - den) for later]
S 4 D [load abs(num)]
S 2#@ [add (den - 1)]
T 4 D [to 4D for division]
[37] A 37 @ [for return from next]
G 110 F [call the above division subroutine]
S 2#@ [load (den - 1)]
S 4 D [subtract remainder]
T 6#@ [result is new denominator]
S 6 D [load negated quotient]
G 26 @ [join common code]
 
[Here if there are no more terms of the c.f.]
[44] T F [clear acc]
A 8 @ [this is negative since 'A' = -4]
G 27 @ [exit with negative flag]
 
[----------------------------------------------------------------------
Main routine]
T 200 K
G K
[Variables]
[0] P F [negative counter of continued fractions]
[1] P F [character before term, first '=' then ',']
[Constants]
[2] P D [single-word 1]
[3] A 2#H [order to load first numerator]
[4] P 2 F [to inc addresses by 2]
[5] # F [teleprinter figures shift]
[6] X F [slash (in figures mode)]
[7] V F [equals sign (in figures mode)]
[8] N F [comma (in figures mode)]
[9] ! F [space]
[10] @ F [carriage return]
[11] & F [line feed]
[12] K4096 F [teleprinter null]
 
[Enter with acc = 0]
[13] O 5 @ [set teleprinter to figures]
S H [negative of number of c.f.s]
T @ [initialize counter]
A 3 @ [initial load order]
[17] U 22 @ [plant order to load numerator]
A 4 @ [inc address by 2]
T 28 @ [plant order to load denominator]
A 7 @ [set to print '=' before first term]
T 1 @
 
[Demonstrate the subroutine above.
Since its address was placed in location 46,
we can use code letter N to refer to it.]
[22] A #H [load numerator (order set up at runtime)]
U 4#N [pass to subroutine]
T D [also to 0D for printing]
[25] A 25 @ [for return from print subroutine]
G 56 F [print numerator]
O 6 @ [followed by slash]
[28] A #H [load denominator (order set up at runtime)]
U 6#N [pass to subroutine]
T D [also to 0D for printing]
[31] A 31 @ [for return from print subroutine]
G 56 F [print denominator]
O 9 @ [followed by space]
[34] A 34 @ [for return from subroutine]
G N [call subroutine for next term]
A 1 N [load flag]
G 48 @ [if < 0, c.f. is finished, jump out]
O 1 @ [print equals or comma]
O 9 @ [print space]
T F [clear acc]
A 2#N [load term]
T D [to 0D for printing]
[43] A 43 @ [for return from print subroutine]
G 56 F [print term; clears acc]
A 8 @ [set to print ',' before subsequent terms]
T 1 @
E 34 @ [loop back for next term]
 
[On to next continued fraction]
[48] O 10 @ [print new line]
O 11 @
T F [clear acc]
A @ [load negative count of c.f.s]
A 2 @ [add 1]
E 59 @ [exit if count = 0]
T @ [store back]
A 22 @ [order to load numerator]
A 4 @ [inc address by 4 for next c.f.]
A 4 @
G 17 @ [loop back (always, since 'A' < 0)]
 
[59] O 12 @ [print null to flush teleprinter buffer]
Z F [stop]
 
E 13 Z [define entry point]
P F [acc = 0 on entry]
</syntaxhighlight>
{{out}}
<pre>
1/2 = 0, 2
3/1 = 3
33/8 = 4, 8
13/11 = 1, 5, 2
22/7 = 3, 7
-151/77 = -2, 25, 1, 2
141421356/100000000 = 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 4, 1, 1, 2, 6, 8
314285714/100000000 = 3, 7, 7142857
</pre>
 
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">let rec r2cf n d =
if d = LanguagePrimitives.GenericZero then []
else let q = n / d in q :: (r2cf d (n - q * d))
Line 400 ⟶ 2,295:
printfn "%A" (r2cf 1414214 1000000)
printfn "%A" (r2cf 14142136 10000000)
0</langsyntaxhighlight>
Output
<pre>[0; 2]
Line 414 ⟶ 2,309:
[1; 2; 2; 2; 2; 2; 2; 2; 3; 6; 1; 2; 1; 12]
[1; 2; 2; 2; 2; 2; 2; 2; 2; 2; 6; 1; 2; 4; 1; 1; 2]</pre>
;A version for larger numerators and denominators.
<syntaxhighlight lang="fsharp">
let rec rI2cf n d =
if d = 0I then []
else let q = n / d in (decimal)q :: (rI2cf d (n - q * d))
</syntaxhighlight>
 
=={{header|Factor}}==
Note that the input values are stored as strings and converted to numbers before being fed to <code>r2cf</code>. This is because ratios automatically reduce themselves to the lowest-terms mixed number, which would make for confusing output in this instance.
<syntaxhighlight lang="factor">USING: formatting kernel lists lists.lazy math math.parser qw
sequences ;
IN: rosetta-code.cf-arithmetic
 
: r2cf ( x -- lazy )
[ >fraction [ /mod ] keep swap [ ] [ / ] if-zero nip ]
lfrom-by [ integer? ] luntil [ >fraction /i ] lmap-lazy ;
 
: main ( -- )
qw{
1/2
3
23/8
13/11
22/7
-151/77
14142/10000
141421/100000
1414214/1000000
14142136/10000000
31/10
314/100
3142/1000
31428/10000
314285/100000
3142857/1000000
31428571/10000000
314285714/100000000
}
[ dup string>number r2cf list>array "%19s -> %u\n" printf ]
each ;
MAIN: main</syntaxhighlight>
{{out}}
<pre>
1/2 -> { 0 2 }
3 -> { 3 }
23/8 -> { 2 1 7 }
13/11 -> { 1 5 2 }
22/7 -> { 3 7 }
-151/77 -> { -1 -1 -24 -1 -2 }
14142/10000 -> { 1 2 2 2 2 2 1 1 29 }
141421/100000 -> { 1 2 2 2 2 2 2 3 1 1 3 1 7 2 }
1414214/1000000 -> { 1 2 2 2 2 2 2 2 3 6 1 2 1 12 }
14142136/10000000 -> { 1 2 2 2 2 2 2 2 2 2 6 1 2 4 1 1 2 }
31/10 -> { 3 10 }
314/100 -> { 3 7 7 }
3142/1000 -> { 3 7 23 1 2 }
31428/10000 -> { 3 7 357 }
314285/100000 -> { 3 7 2857 }
3142857/1000000 -> { 3 7 142857 }
31428571/10000000 -> { 3 7 476190 3 }
314285714/100000000 -> { 3 7 7142857 }
</pre>
 
=={{header|Forth}}==
 
{{works with|gforth|0.7.3}}
 
<syntaxhighlight lang="forth">: r2cf ( num1 den1 -- num2 den2 ) swap over >r s>d r> sm/rem . ;
 
: .r2cf ( num den -- )
cr 2dup swap . ." / " . ." : "
begin
r2cf dup 0<> while
repeat 2drop ;
 
: r2cf-demo
1 2 .r2cf
3 1 .r2cf
23 8 .r2cf
13 11 .r2cf
22 7 .r2cf
-151 77 .r2cf
14142 10000 .r2cf
141421 100000 .r2cf
1414214 1000000 .r2cf
14142136 10000000 .r2cf
31 10 .r2cf
314 100 .r2cf
3142 1000 .r2cf
31428 10000 .r2cf
314285 100000 .r2cf
3142857 1000000 .r2cf
31428571 10000000 .r2cf
314285714 100000000 .r2cf
3141592653589793 1000000000000000 .r2cf ;
r2cf-demo</syntaxhighlight>
 
{{out}}
<pre>1 / 2 : 0 2
3 / 1 : 3
23 / 8 : 2 1 7
13 / 11 : 1 5 2
22 / 7 : 3 7
-151 / 77 : -1 -1 -24 -1 -2
14142 / 10000 : 1 2 2 2 2 2 1 1 29
141421 / 100000 : 1 2 2 2 2 2 2 3 1 1 3 1 7 2
1414214 / 1000000 : 1 2 2 2 2 2 2 2 3 6 1 2 1 12
14142136 / 10000000 : 1 2 2 2 2 2 2 2 2 2 6 1 2 4 1 1 2
31 / 10 : 3 10
314 / 100 : 3 7 7
3142 / 1000 : 3 7 23 1 2
31428 / 10000 : 3 7 357
314285 / 100000 : 3 7 2857
3142857 / 1000000 : 3 7 142857
31428571 / 10000000 : 3 7 476190 3
314285714 / 100000000 : 3 7 7142857
3141592653589793 / 1000000000000000 : 3 7 15 1 292 1 1 1 2 1 3 1 14 4 2 3 1 12 5 1 5 20 1 11 1 1 1 2 ok</pre>
 
 
 
=={{header|Fortran}}==
<syntaxhighlight lang="f90">
program r2cf_demo
implicit none
 
call write_r2cf (1, 2)
call write_r2cf (3, 1)
call write_r2cf (23, 8)
call write_r2cf (13, 11)
call write_r2cf (22, 7)
call write_r2cf (-151, 77)
 
call write_r2cf (14142, 10000)
call write_r2cf (141421, 100000)
call write_r2cf (1414214, 1000000)
call write_r2cf (14142136, 10000000)
 
call write_r2cf (31, 10)
call write_r2cf (314, 100)
call write_r2cf (3142, 1000)
call write_r2cf (31428, 10000)
call write_r2cf (314285, 100000)
call write_r2cf (3142857, 1000000)
call write_r2cf (31428571, 10000000)
call write_r2cf (314285714, 100000000)
 
contains
 
! This implementation of r2cf both modifies its arguments and
! returns a value.
function r2cf (N1, N2) result (q)
integer, intent(inout) :: N1, N2
integer :: q
 
integer r
 
! We will use floor division, where the quotient is rounded
! towards negative infinity. Whenever the divisor is positive,
! this type of division gives a non-negative remainder.
r = modulo (N1, N2)
q = (N1 - r) / N2
 
N1 = N2
N2 = r
end function r2cf
 
subroutine write_r2cf (N1, N2)
integer, intent(in) :: N1, N2
 
integer :: digit, M1, M2
character(len = :), allocatable :: sep
 
write (*, '(I0, "/", I0, " => ")', advance = "no") N1, N2
 
M1 = N1
M2 = N2
sep = "["
do while (M2 /= 0)
digit = r2cf (M1, M2)
write (*, '(A, I0)', advance = "no") sep, digit
if (sep == "[") then
sep = "; "
else
sep = ", "
end if
end do
write (*, '("]")', advance = "yes")
end subroutine write_r2cf
 
end program r2cf_demo
</syntaxhighlight>
 
{{out}}
<pre>$ gfortran -std=f2018 continued-fraction-from-rational.f90 && ./a.out
1/2 => [0; 2]
3/1 => [3]
23/8 => [2; 1, 7]
13/11 => [1; 5, 2]
22/7 => [3; 7]
-151/77 => [-2; 25, 1, 2]
14142/10000 => [1; 2, 2, 2, 2, 2, 1, 1, 29]
141421/100000 => [1; 2, 2, 2, 2, 2, 2, 3, 1, 1, 3, 1, 7, 2]
1414214/1000000 => [1; 2, 2, 2, 2, 2, 2, 2, 3, 6, 1, 2, 1, 12]
14142136/10000000 => [1; 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 1, 2, 4, 1, 1, 2]
31/10 => [3; 10]
314/100 => [3; 7, 7]
3142/1000 => [3; 7, 23, 1, 2]
31428/10000 => [3; 7, 357]
314285/100000 => [3; 7, 2857]
3142857/1000000 => [3; 7, 142857]
31428571/10000000 => [3; 7, 476190, 3]
314285714/100000000 => [3; 7, 7142857]</pre>
 
=={{header|Go}}==
(Note, the files making up this package are re-used as presented here for for the
[[Continued_fraction/Arithmetic/G(matrix_NG,_Contined_Fraction_N)#Go]]
and
[[Continued_fraction/Arithmetic/G(matrix_NG,_Contined_Fraction_N1,_Contined_Fraction_N2)#Go]]
tasks.)
 
File <code>cf.go</code>:
<syntaxhighlight lang="go">package cf
 
import (
"fmt"
"strings"
)
 
// ContinuedFraction is a regular continued fraction.
type ContinuedFraction func() NextFn
 
// NextFn is a function/closure that can return
// a posibly infinite sequence of values.
type NextFn func() (term int64, ok bool)
 
// String implements fmt.Stringer.
// It formats a maximum of 20 values, ending the
// sequence with ", ..." if the sequence is longer.
func (cf ContinuedFraction) String() string {
var buf strings.Builder
buf.WriteByte('[')
sep := "; "
const maxTerms = 20
next := cf()
for n := 0; ; n++ {
t, ok := next()
if !ok {
break
}
if n > 0 {
buf.WriteString(sep)
sep = ", "
}
if n >= maxTerms {
buf.WriteString("...")
break
}
fmt.Fprint(&buf, t)
}
buf.WriteByte(']')
return buf.String()
}
 
// Sqrt2 is the continued fraction for √2, [1; 2, 2, 2, ...].
func Sqrt2() NextFn {
first := true
return func() (int64, bool) {
if first {
first = false
return 1, true
}
return 2, true
}
}
 
// Phi is the continued fraction for ϕ, [1; 1, 1, 1, ...].
func Phi() NextFn {
return func() (int64, bool) { return 1, true }
}
 
// E is the continued fraction for e,
// [2; 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10, 1, 1, 12, ...].
func E() NextFn {
var i int
return func() (int64, bool) {
i++
switch {
case i == 1:
return 2, true
case i%3 == 0:
return int64(i/3) * 2, true
default:
return 1, true
}
}
}</syntaxhighlight>
File <code>rat.go</code>:
<syntaxhighlight lang="go">package cf
 
import "fmt"
 
// A Rat represents a quotient N/D.
type Rat struct {
N, D int64
}
 
// String implements fmt.Stringer and returns a string
// representation of `r` in the form "N/D" (even if D == 1).
func (r Rat) String() string {
return fmt.Sprintf("%d/%d", r.N, r.D)
}
 
// As ContinuedFraction returns a contined fraction representation of `r`.
func (r Rat) AsContinuedFraction() ContinuedFraction { return r.CFTerms }
func (r Rat) CFTerms() NextFn {
return func() (int64, bool) {
if r.D == 0 {
return 0, false
}
q := r.N / r.D
r.N, r.D = r.D, r.N-q*r.D
return q, true
}
}
 
// Rosetta Code task explicitly asked for this function,
// so here it is. We'll just use the types above instead.
func r2cf(n1, n2 int64) ContinuedFraction { return Rat{n1, n2}.CFTerms }</syntaxhighlight>
File <code>rat_test.go</code>:
<syntaxhighlight lang="go">package cf
 
import (
"fmt"
"math"
)
 
func Example_ConstructFromRational() {
cases := [...]Rat{
{1, 2},
{3, 1},
{23, 8},
{13, 11},
{22, 7},
{-151, 77},
}
for _, r := range cases {
fmt.Printf("%7s = %s\n", r, r.AsContinuedFraction())
}
 
for _, tc := range [...]struct {
name string
approx float64
cf ContinuedFraction
d1, d2 int64
}{
{"√2", math.Sqrt2, Sqrt2, 1e4, 1e8},
{"π", math.Pi, nil, 10, 1e10},
{"ϕ", math.Phi, Phi, 10, 1e5},
{"e", math.E, E, 1e5, 1e9},
} {
fmt.Printf("\nApproximating %s ≅ %v:\n", tc.name, tc.approx)
for d := tc.d1; d < tc.d2; d *= 10 {
n := int64(math.Round(tc.approx * float64(d)))
r := Rat{n, d}
fmt.Println(r, "=", r.AsContinuedFraction())
}
if tc.cf != nil {
wid := int(math.Log10(float64(tc.d2)))*2 + 2 // ick
fmt.Printf("%*s: %v\n", wid, "Actual", tc.cf)
}
}
 
// Output:
// [… commented output used by go test omitted for
// Rosetta Code listing; it is the same as below …]
}</syntaxhighlight>
{{out}}
<pre>
1/2 = [0; 2]
3/1 = [3]
23/8 = [2; 1, 7]
13/11 = [1; 5, 2]
22/7 = [3; 7]
-151/77 = [-1; -1, -24, -1, -2]
 
Approximating √2 ≅ 1.4142135623730951:
14142/10000 = [1; 2, 2, 2, 2, 2, 1, 1, 29]
141421/100000 = [1; 2, 2, 2, 2, 2, 2, 3, 1, 1, 3, 1, 7, 2]
1414214/1000000 = [1; 2, 2, 2, 2, 2, 2, 2, 3, 6, 1, 2, 1, 12]
14142136/10000000 = [1; 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 1, 2, 4, 1, 1, 2]
Actual: [1; 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, ...]
 
Approximating π ≅ 3.141592653589793:
31/10 = [3; 10]
314/100 = [3; 7, 7]
3142/1000 = [3; 7, 23, 1, 2]
31416/10000 = [3; 7, 16, 11]
314159/100000 = [3; 7, 15, 1, 25, 1, 7, 4]
3141593/1000000 = [3; 7, 16, 983, 4, 2]
31415927/10000000 = [3; 7, 15, 1, 354, 2, 6, 1, 4, 1, 2]
314159265/100000000 = [3; 7, 15, 1, 288, 1, 2, 1, 3, 1, 7, 4]
3141592654/1000000000 = [3; 7, 15, 1, 293, 11, 1, 1, 7, 2, 1, 3, 3, 2]
 
Approximating ϕ ≅ 1.618033988749895:
16/10 = [1; 1, 1, 2]
162/100 = [1; 1, 1, 1, 1, 1, 2, 2]
1618/1000 = [1; 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5]
16180/10000 = [1; 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5]
Actual: [1; 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ...]
 
Approximating e ≅ 2.718281828459045:
271828/100000 = [2; 1, 2, 1, 1, 4, 1, 1, 6, 10, 1, 1, 2]
2718282/1000000 = [2; 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 3, 141]
27182818/10000000 = [2; 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 11, 1, 2, 10, 6, 2]
271828183/100000000 = [2; 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 2, 1, 1, 17, 6, 1, 1, 1, ...]
Actual: [2; 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10, 1, 1, 12, 1, 1, ...]
</pre>
 
=={{header|Haskell}}==
{{trans|Python}}
This more general version generates a continued fraction from any real number (with rationals as a special case):
<langsyntaxhighlight lang="haskell">import Data.Ratio ((%))
 
real2cf :: (RealFrac a, Integral b) => a -> [b]
real2cf x =
let (i, f) = properFraction x
i : if f == 0 then [] else real2cf (1/f)
in i :
where (i, f) = properFraction x
if f == 0
then []
else real2cf (1 / f)
 
main :: IO ()
main = do
mapM_
print $ real2cf (13 % 11) -- => [1,5,2]
print
print $ take 20 $ real2cf (sqrt 2) -- => [1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]</lang>
[ real2cf (13 % 11)
, take 20 $ real2cf (sqrt 2)
]</syntaxhighlight>
{{Out}}
<pre>[1,5,2]
[1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]</pre>
 
=={{header|Icon}}==
 
Here, for each test case, '''r2cf''' is really "called" only once, but then suspended and resumed repeatedly.
 
<syntaxhighlight lang="icon">
procedure main ()
write_r2cf (1, 2)
write_r2cf (3, 1)
write_r2cf (23, 8)
write_r2cf (13, 11)
write_r2cf (22, 7)
write_r2cf (-151, 77)
write_r2cf (14142, 10000)
write_r2cf (141421, 100000)
write_r2cf (1414214, 1000000)
write_r2cf (14142136, 10000000)
 
# Decimal expansion of sqrt(2): https://oeis.org/A002193
write_r2cf (141421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157,
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000)
 
write_r2cf (31, 10)
write_r2cf (314, 100)
write_r2cf (3142, 1000)
write_r2cf (31428, 10000)
write_r2cf (314285, 100000)
write_r2cf (3142857, 1000000)
write_r2cf (31428571, 10000000)
write_r2cf (314285714, 100000000)
 
# 22/7 = 3 + 1/7 = 3 + 0.142857...
write_r2cf (3142857142857142857142857142857142857142857142857142857142857142857142857142857142857142857142857,
1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000)
end
 
procedure write_r2cf (N1, N2)
local sep, digit
 
writes (N1, "/", N2, " => ")
sep := "["
every digit := r2cf (N1, N2) do {
writes (sep, digit)
sep := (if sep == "[" then "; " else ", ")
}
write ("]")
end
 
procedure r2cf (N1, N2)
local q, r
while N2 ~= 0 do {
# We will use Icon's native version of integer division, which
# rounds quotients towards zero, and so may return a negative
# remainder.
q := N1 / N2
r := N1 % N2
N1 := N2
N2 := r
suspend q
}
end
</syntaxhighlight>
 
{{out}}
 
<pre> $ icon continued-fraction-from-rational.icn
1/2 => [0; 2]
3/1 => [3]
23/8 => [2; 1, 7]
13/11 => [1; 5, 2]
22/7 => [3; 7]
-151/77 => [-1; -1, -24, -1, -2]
14142/10000 => [1; 2, 2, 2, 2, 2, 1, 1, 29]
141421/100000 => [1; 2, 2, 2, 2, 2, 2, 3, 1, 1, 3, 1, 7, 2]
1414214/1000000 => [1; 2, 2, 2, 2, 2, 2, 2, 3, 6, 1, 2, 1, 12]
14142136/10000000 => [1; 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 1, 2, 4, 1, 1, 2]
141421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157/100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 => [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, 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, 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, 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, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 5, 1, 8, 3, 1, 2, 2, 1, 6, 2, 2, 3, 1, 2, 3, 1, 1, 39, 1, 3, 1, 2, 4, 10, 1, 6, 1, 30, 5, 2, 1, 1, 1, 1, 1, 32, 1, 4, 18, 124, 3, 2, 1, 1, 8, 1, 1, 1, 6, 15, 2, 3, 2, 7, 1, 4, 9, 2, 7, 1, 1, 1, 1, 1, 2, 1, 10, 1, 31, 5, 1, 1, 1, 7, 1, 14, 10, 3, 11, 1, 2, 1, 65, 4, 9, 2, 3, 2, 2, 9, 1, 1, 2, 1, 1, 2]
31/10 => [3; 10]
314/100 => [3; 7, 7]
3142/1000 => [3; 7, 23, 1, 2]
31428/10000 => [3; 7, 357]
314285/100000 => [3; 7, 2857]
3142857/1000000 => [3; 7, 142857]
31428571/10000000 => [3; 7, 476190, 3]
314285714/100000000 => [3; 7, 7142857]
3142857142857142857142857142857142857142857142857142857142857142857142857142857142857142857142857/1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 => [3; 7, 142857142857142857142857142857142857142857142857142857142857142857142857142857142857142857142857]</pre>
 
=={{header|J}}==
Line 436 ⟶ 2,844:
 
This version is a modification of an explicit version shown in http://www.jsoftware.com/jwiki/Essays/Continued%20Fractions to comply with the task specifications.
<langsyntaxhighlight lang="j">cf=: _1 1 ,@}. (, <.)@%@-/ ::]^:a:@(, <.)@(%&x:/)</langsyntaxhighlight>
==== Examples ====
<langsyntaxhighlight lang="j"> cf each 1 2;3 1;23 8;13 11;22 7;14142136 10000000;_151 77
┌───┬─┬─────┬─────┬───┬─────────────────────────────────┬─────────┐
│0 2│3│2 1 7│1 5 2│3 7│1 2 2 2 2 2 2 2 2 2 6 1 2 4 1 1 2│_2 25 1 2│
Line 449 ⟶ 2,857:
┌────┬─────┬──────────┬───────┬────────┬──────────┬────────────┬───────────┐
│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│
└────┴─────┴──────────┴───────┴────────┴──────────┴────────────┴───────────┘</langsyntaxhighlight>
This tacit version first produces the answer with a trailing ∞ (represented by _ in J) which is then removed by the last operation (_1 1 ,@}. ...). A continued fraction can be evaluated using the verb ((+%)/) and both representations produce equal results,
<langsyntaxhighlight lang="j"> 3 7 =&((+ %)/) 3 7 _
1</langsyntaxhighlight>
Incidentally, J and Tcl report a different representation for -151/77 versus the representation of some other implementations; however, both representations produce equal results.
<langsyntaxhighlight lang="j"> _2 25 1 2 =&((+ %)/) _1 _1 _24 _1 _2
1</langsyntaxhighlight>
 
===Tacit version 2===
Line 461 ⟶ 2,869:
Translation of python
 
<langsyntaxhighlight Jlang="j">r2cf=:1 1{."1@}.({:,(0,{:)#:{.)^:(*@{:)^:a:</langsyntaxhighlight>
 
Example use:
 
<langsyntaxhighlight Jlang="j"> ((":@{.,'/',":@{:),': ',":@r2cf)@>1 2;3 1;23 8;13 11;22 7;14142136 10000000;_151 77;14142 10000;141421 100000;1414214 1000000;14142136 10000000;31 10;314 100;3142 1000;31428 10000;314285 100000;3142857 1000000;31428571 10000000;314285714 100000000
1/2: 0 2
3/1: 3
Line 484 ⟶ 2,892:
3142857/1000000: 3 7 142857
31428571/10000000: 3 7 476190 3
314285714/100000000: 3 7 7142857 </langsyntaxhighlight>
 
===Explicit versions===
==== version 1 ====
Implemented as a class, r2cf preserves state in a separate locale. I've used some contrivances to jam the examples onto one line.
<syntaxhighlight lang="j">
<lang J>
coclass'cf'
create =: dyad def 'EMPTY [ N =: x , y'
Line 522 ⟶ 2,930:
│_151 77 │_2 25 1 2 │
└─────────────────┴─────────────────────────────────┘
)</langsyntaxhighlight>
==== version 2 ====
<syntaxhighlight lang="j">
<lang J>
f =: 3 : 0
a =. {.y
Line 537 ⟶ 2,945:
┌───┬─┬─────┬─────┬───┬───────────────────────────────────┬─────────┐
│0 2│3│2 1 7│1 5 2│3 7│1 2 2 2 2 2 2 2 2 2 6 1 2 4 1 1 2 _│_2 25 1 2│
└───┴─┴─────┴─────┴───┴───────────────────────────────────┴─────────┘</langsyntaxhighlight>
 
==== version 3 ====
Line 543 ⟶ 2,951:
translation of python:
 
<langsyntaxhighlight Jlang="j">r2cf=:3 :0
'n1 n2'=. y
r=.''
Line 550 ⟶ 2,958:
r=.r,t1
end.
)</langsyntaxhighlight>
 
Example:
 
<langsyntaxhighlight Jlang="j"> r2cf each 1 2;3 1;23 8;13 11;22 7;14142136 10000000;_151 77
┌───┬─┬─────┬─────┬───┬─────────────────────────────────┬─────────┐
│0 2│3│2 1 7│1 5 2│3 7│1 2 2 2 2 2 2 2 2 2 6 1 2 4 1 1 2│_2 25 1 2│
└───┴─┴─────┴─────┴───┴─────────────────────────────────┴─────────┘</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|Kotlin}}
{{works with|Java|9}}
<syntaxhighlight lang="java">import java.util.Iterator;
import java.util.List;
import java.util.Map;
 
public class ConstructFromRationalNumber {
private static class R2cf implements Iterator<Integer> {
private int num;
private int den;
 
R2cf(int num, int den) {
this.num = num;
this.den = den;
}
 
@Override
public boolean hasNext() {
return den != 0;
}
 
@Override
public Integer next() {
int div = num / den;
int rem = num % den;
num = den;
den = rem;
return div;
}
}
 
private static void iterate(R2cf generator) {
generator.forEachRemaining(n -> System.out.printf("%d ", n));
System.out.println();
}
 
public static void main(String[] args) {
List<Map.Entry<Integer, Integer>> fracs = List.of(
Map.entry(1, 2),
Map.entry(3, 1),
Map.entry(23, 8),
Map.entry(13, 11),
Map.entry(22, 7),
Map.entry(-151, 77)
);
for (Map.Entry<Integer, Integer> frac : fracs) {
System.out.printf("%4d / %-2d = ", frac.getKey(), frac.getValue());
iterate(new R2cf(frac.getKey(), frac.getValue()));
}
 
System.out.println("\nSqrt(2) ->");
List<Map.Entry<Integer, Integer>> root2 = List.of(
Map.entry( 14_142, 10_000),
Map.entry( 141_421, 100_000),
Map.entry( 1_414_214, 1_000_000),
Map.entry(14_142_136, 10_000_000)
);
for (Map.Entry<Integer, Integer> frac : root2) {
System.out.printf("%8d / %-8d = ", frac.getKey(), frac.getValue());
iterate(new R2cf(frac.getKey(), frac.getValue()));
}
 
System.out.println("\nPi ->");
List<Map.Entry<Integer, Integer>> pi = List.of(
Map.entry( 31, 10),
Map.entry( 314, 100),
Map.entry( 3_142, 1_000),
Map.entry( 31_428, 10_000),
Map.entry( 314_285, 100_000),
Map.entry( 3_142_857, 1_000_000),
Map.entry( 31_428_571, 10_000_000),
Map.entry(314_285_714, 100_000_000)
);
for (Map.Entry<Integer, Integer> frac : pi) {
System.out.printf("%9d / %-9d = ", frac.getKey(), frac.getValue());
iterate(new R2cf(frac.getKey(), frac.getValue()));
}
}
}</syntaxhighlight>
{{out}}
<pre> 1 / 2 = 0 2
3 / 1 = 3
23 / 8 = 2 1 7
13 / 11 = 1 5 2
22 / 7 = 3 7
-151 / 77 = -1 -1 -24 -1 -2
 
Sqrt(2) ->
14142 / 10000 = 1 2 2 2 2 2 1 1 29
141421 / 100000 = 1 2 2 2 2 2 2 3 1 1 3 1 7 2
1414214 / 1000000 = 1 2 2 2 2 2 2 2 3 6 1 2 1 12
14142136 / 10000000 = 1 2 2 2 2 2 2 2 2 2 6 1 2 4 1 1 2
 
Pi ->
31 / 10 = 3 10
314 / 100 = 3 7 7
3142 / 1000 = 3 7 23 1 2
31428 / 10000 = 3 7 357
314285 / 100000 = 3 7 2857
3142857 / 1000000 = 3 7 142857
31428571 / 10000000 = 3 7 476190 3
314285714 / 100000000 = 3 7 7142857
</pre>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
<lang Julia># It's most appropriate to define a Julia iterable object for this task
 
# Julia doesn't have Python's yield, the closest to it is produce/consume calls with Julia tasks
<syntaxhighlight lang="julia"># It'st most appropriate to define a Julia iterable object for this task
# Julia doesn't have Python'st yield, the closest to it is produce/consume calls with Julia tasks
# but for various reasons they don't work out for this task
# This solution works with two integers, a Julia rational or a real
 
mutable struct ContinuedFraction{T<:Integer}
type R2cf
n1::Union{Int,Float64}T # numerator or real
n2::IntT # denominator or 1 if real
t1::IntT # generated coefficient
f::Float64 # aux. field for working with real inputs
end
 
# constructorsConstructors for all possible input types
ContinuedFraction{T<:Integer}(n1::T, n2::T) = ContinuedFraction(n1, n2, 0)
R2cf(n::Union{Int,Float64})=R2cf(n,1,0,0.1)
ContinuedFraction(n::Rational) = ContinuedFraction(numerator(n), denominator(n))
R2cf(n1::Int,n2::Int)=R2cf(n1,n2,0,0.1)
ContinuedFraction(n::AbstractFloat) = ContinuedFraction(Rational(n))
R2cf(r::Rational{Int})=R2cf(num(r),den(r),0,0.1)
 
# methodsMethods to make our object iterable
Base.start(::R2cfContinuedFraction) = nothing
# Returns true if we've prepared the continued fraction
 
Base.done(cf::ContinuedFraction, st) = cf.n2 == 0
# generates the next coefficient
# Generates the next coefficient
function Base.next(cf::R2cf,s)
function Base.next(cf::ContinuedFraction, st)
if typeof(cf.n1)==Int
cf.n1, (cf.t1, cf.n2) = cf.n2, divrem(cf.n1, cf.n2)
return cf.t1, nothing
else
cf.t1,cf.f=divrem(cf.n1,1)
if cf.f!=0.0 cf.n1=1/cf.f end
end
return (cf.t1,nothing)
end
 
# Tell Julia that this object always returns ints (all coeffs are integers)
# returns true if we've prepared the continued fraction
Base.eltype{T}(::Type{ContinuedFraction{T}}) = T
function Base.done(cf::R2cf,s)
if typeof(cf.n1)==Int
return cf.n2==0
else
return cf.f==0.0
end
end
 
# Overload the default collect function so that we can collect the first maxiter coeffs of infinite continued fractions
# tell Julia that this object always returns ints (all coeffs are integers)
Base.eltype(::Type{R2cf})=Int
 
# overload the default collect function so that we can collect the first maxiter coeffs of infinite continued fractions
# array slicing doesn't work as Julia crashes before the slicing due to our infinitely long array
function Base.collect(itr::R2cfContinuedFraction, maxiter::IntInteger = 100)
r = Array{eltype(itr)}(maxiter)
r=Array{Int,1}() # all results are ints
i = 1
for v= in itr
r[i] = v
push!(r,v)
i += 1
if i== > maxiter break end
end
return r[1:i-1]
end
 
# testTest cases according to task description with outputs in comments
println(collect(R2cfContinuedFraction(1, 2))) # => [0, 2]
println(collect(R2cfContinuedFraction(3, 1))) # => [3]
println(collect(R2cfContinuedFraction(23, 8))) # => [2, 1, 7]
println(collect(R2cfContinuedFraction(13, 11))) # => [1, 5, 2]
println(collect(R2cfContinuedFraction(22, 7))) # => [3, 7]
println(collect(R2cfContinuedFraction(14142, 10000))) # => [1, 2, 2, 2, 2, 2, 1, 1, 29]
println(collect(R2cfContinuedFraction(141421, 100000))) # => [1, 2, 2, 2, 2, 2, 2, 3, 1, 1, 3, 1, 7, 2]
println(collect(R2cfContinuedFraction(1414214, 1000000))) # => [1, 2, 2, 2, 2, 2, 2, 2, 3, 6, 1, 2, 1, 12]
println(collect(R2cfContinuedFraction(14142136, 10000000))) # => [1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 1, 2, 4, 1, 1, 2]
 
println(collect(ContinuedFraction(13 // 11))) # => [1, 5, 2]
println(collect(ContinuedFraction(√2), 20)) # => [1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]</syntaxhighlight>
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">// version 1.1.2
// compile with -Xcoroutines=enable flag from command line
 
import kotlin.coroutines.experimental.buildSequence
 
fun r2cf(frac: Pair<Int, Int>) =
buildSequence {
var num = frac.first
var den = frac.second
while (Math.abs(den) != 0) {
val div = num / den
val rem = num % den
num = den
den = rem
yield(div)
}
}
 
fun iterate(seq: Sequence<Int>) {
for (i in seq) print("$i ")
println()
}
 
fun main(args: Array<String>) {
val fracs = arrayOf(1 to 2, 3 to 1, 23 to 8, 13 to 11, 22 to 7, -151 to 77)
for (frac in fracs) {
print("${"%4d".format(frac.first)} / ${"%-2d".format(frac.second)} = ")
iterate(r2cf(frac))
}
val root2 = arrayOf(14142 to 10000, 141421 to 100000,
1414214 to 1000000, 14142136 to 10000000)
println("\nSqrt(2) ->")
for (frac in root2) {
print("${"%8d".format(frac.first)} / ${"%-8d".format(frac.second)} = ")
iterate(r2cf(frac))
}
val pi = arrayOf(31 to 10, 314 to 100, 3142 to 1000, 31428 to 10000,
314285 to 100000, 3142857 to 1000000,
31428571 to 10000000, 314285714 to 100000000)
println("\nPi ->")
for (frac in pi) {
print("${"%9d".format(frac.first)} / ${"%-9d".format(frac.second)} = ")
iterate(r2cf(frac))
}
}</syntaxhighlight>
 
{{out}}
<pre>
1 / 2 = 0 2
3 / 1 = 3
23 / 8 = 2 1 7
13 / 11 = 1 5 2
22 / 7 = 3 7
-151 / 77 = -1 -1 -24 -1 -2
 
Sqrt(2) ->
14142 / 10000 = 1 2 2 2 2 2 1 1 29
141421 / 100000 = 1 2 2 2 2 2 2 3 1 1 3 1 7 2
1414214 / 1000000 = 1 2 2 2 2 2 2 2 3 6 1 2 1 12
14142136 / 10000000 = 1 2 2 2 2 2 2 2 2 2 6 1 2 4 1 1 2
 
Pi ->
31 / 10 = 3 10
314 / 100 = 3 7 7
3142 / 1000 = 3 7 23 1 2
31428 / 10000 = 3 7 357
314285 / 100000 = 3 7 2857
3142857 / 1000000 = 3 7 142857
31428571 / 10000000 = 3 7 476190 3
314285714 / 100000000 = 3 7 7142857
</pre>
 
=={{header|m4}}==
 
Being able to do such things in m4, without much trouble, makes it very useful as a program-source preprocessor. There may be better macroprocessors, but having ''some'' version of m4 is standard for POSIX platforms.
 
<syntaxhighlight lang="m4">divert(-1)
 
# m4 is a recursive macro language with eager evaluation. Generally
# there is no tail-call optimization. I shall define r2cf in a natural
# way, rather than try to mimic call-by-reference or lazy evaluation.
 
define(`r2cf',`$1/$2 => [_$0($1,$2,`')]')
define(`_r2cf',
`ifelse(eval($2 != 0),1,
`$3eval($1 / $2)$0($2,eval($1 % $2),ifelse($3,,`; ',```,'' '))')')
 
divert`'dnl
dnl
r2cf(1, 2)
r2cf(3, 1)
r2cf(23, 8)
r2cf(13, 11)
r2cf(22, 7)
r2cf(-151, 77)
dnl
r2cf(14142, 10000)
r2cf(141421, 100000)
r2cf(1414214, 1000000)
r2cf(14142136, 10000000)
dnl
r2cf(31, 10)
r2cf(314, 100)
r2cf(3142, 1000)
r2cf(31428, 10000)
r2cf(314285, 100000)
r2cf(3142857, 1000000)
r2cf(31428571, 10000000)
r2cf(314285714, 100000000)
</syntaxhighlight>
 
{{out}}
 
<pre>$ m4 continued-fraction-from-rational.m4
1/2 => [0; 2]
3/1 => [3]
23/8 => [2; 1, 7]
13/11 => [1; 5, 2]
22/7 => [3; 7]
-151/77 => [-1; -1, -24, -1, -2]
14142/10000 => [1; 2, 2, 2, 2, 2, 1, 1, 29]
141421/100000 => [1; 2, 2, 2, 2, 2, 2, 3, 1, 1, 3, 1, 7, 2]
1414214/1000000 => [1; 2, 2, 2, 2, 2, 2, 2, 3, 6, 1, 2, 1, 12]
14142136/10000000 => [1; 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 1, 2, 4, 1, 1, 2]
31/10 => [3; 10]
314/100 => [3; 7, 7]
3142/1000 => [3; 7, 23, 1, 2]
31428/10000 => [3; 7, 357]
314285/100000 => [3; 7, 2857]
3142857/1000000 => [3; 7, 142857]
31428571/10000000 => [3; 7, 476190, 3]
314285714/100000000 => [3; 7, 7142857]</pre>
 
=={{header|Make}}==
===GNU Make and POSIX shell===
 
The '''r2cf''' GNU Make function both computes and prints the digits, one by one, via a POSIX shell loop.
 
<syntaxhighlight lang="make">
.SILENT:
.DEFAULT_GOAL := start-here
 
define r2cf =
M=`expr $(1)`; \
N=`expr $(2)`; \
printf '%d/%d => ' $$M $$N; \
SEP='['; \
while test $$N -ne 0; do \
printf "%s%d" "$$SEP" `expr $$M '/' $$N`; \
if test "$$SEP" = '['; then SEP='; '; else SEP=', '; fi; \
R=`expr $$M '%' $$N`; \
M=$$N; \
N=$$R; \
done; \
printf ']\n'
endef
 
start-here:
$(call r2cf, 1, 2)
$(call r2cf, 3, 1)
$(call r2cf, 23, 8)
$(call r2cf, 13, 11)
$(call r2cf, 22, 7)
$(call r2cf, -151, 77)
 
$(call r2cf, 14142, 10000)
$(call r2cf, 141421, 100000)
$(call r2cf, 1414214, 1000000)
$(call r2cf, 14142136, 10000000)
 
$(call r2cf, 31, 10)
$(call r2cf, 314, 100)
$(call r2cf, 3142, 1000)
$(call r2cf, 31428, 10000)
$(call r2cf, 314285, 100000)
$(call r2cf, 3142857, 1000000)
$(call r2cf, 31428571, 10000000)
$(call r2cf, 314285714, 100000000)
</syntaxhighlight>
 
{{out}}
 
<pre>$ make -f continued-fraction-from-rational.mk
1/2 => [0; 2]
3/1 => [3]
23/8 => [2; 1, 7]
13/11 => [1; 5, 2]
22/7 => [3; 7]
-151/77 => [-1; -1, -24, -1, -2]
14142/10000 => [1; 2, 2, 2, 2, 2, 1, 1, 29]
141421/100000 => [1; 2, 2, 2, 2, 2, 2, 3, 1, 1, 3, 1, 7, 2]
1414214/1000000 => [1; 2, 2, 2, 2, 2, 2, 2, 3, 6, 1, 2, 1, 12]
14142136/10000000 => [1; 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 1, 2, 4, 1, 1, 2]
31/10 => [3; 10]
314/100 => [3; 7, 7]
3142/1000 => [3; 7, 23, 1, 2]
31428/10000 => [3; 7, 357]
314285/100000 => [3; 7, 2857]
3142857/1000000 => [3; 7, 142857]
31428571/10000000 => [3; 7, 476190, 3]
314285714/100000000 => [3; 7, 7142857]</pre>
 
println(collect(R2cf(13//11))) # => [1, 5, 2]
println(collect(R2cf(2 ^ 0.5), 20)) # => [1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]</lang>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Mathematica has a build-in function ContinuedFraction.
<langsyntaxhighlight lang="mathematica">ContinuedFraction[1/2]
ContinuedFraction[3]
ContinuedFraction[23/8]
Line 640 ⟶ 3,345:
ContinuedFraction[141421/100000]
ContinuedFraction[1414214/1000000]
ContinuedFraction[14142136/10000000]</langsyntaxhighlight>
{{Out}}
<pre>{0, 2}
Line 652 ⟶ 3,357:
{1, 2, 2, 2, 2, 2, 2, 2, 3, 6, 1, 2, 1, 12}
{1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 1, 2, 4, 1, 1, 2}</pre>
 
=={{header|Mercury}}==
===A "straightforward" implementation===
{{works with|Mercury|22.01.1}}
<syntaxhighlight lang="mercury">%%%-------------------------------------------------------------------
 
:- module continued_fraction_from_rational.
 
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
 
:- implementation.
:- import_module int.
:- import_module list.
:- import_module string.
 
%%%-------------------------------------------------------------------
%%%
%%% ‘r2cf’ is a predicate, not a function. If it succeeds, it
%%% calculates not only the next digit, but the next starting
%%% fraction. If it fails, we are done.
%%%
 
:- pred r2cf(int::in, int::out, int::in, int::out, int::out)
is semidet.
r2cf(!N1, !N2, Digit) :-
(Dividend = !.N1),
(Divisor = !.N2),
(Divisor \= 0), % Fail if we have reached the end.
(!:N1 = Divisor), % The next Dividend.
(!:N2 = Dividend mod Divisor), % Floor division. The next divisor.
(Digit = Dividend div Divisor). % Floor division. The next digit.
 
%%%-------------------------------------------------------------------
%%%
%%% ‘r2cf_digits’ takes numerator and denominator of a rational
%%% number, and returns a list of continued fraction digits.
%%%
 
:- func r2cf_digits(int, int) = list(int).
:- pred r2cf_digits_loop(int::in, int::in,
list(int)::in, list(int)::out) is det.
r2cf_digits(N1, N2) = Digit_list :-
r2cf_digits_loop(N1, N2, [], Digit_list).
r2cf_digits_loop(N1, N2, !Digit_list) :-
(if r2cf(N1, N1_next, N2, N2_next, Digit)
then r2cf_digits_loop(N1_next, N2_next,
[Digit | !.Digit_list],
!:Digit_list)
else (!:Digit_list = reverse(!.Digit_list))).
 
%%%-------------------------------------------------------------------
%%%
%%% ‘print_cf’ prints a continued fraction nicely.
%%%
 
:- pred print_cf(list(int)::in, io::di, io::uo) is det.
:- pred print_cf_loop(list(int)::in, string::in, io::di, io::uo)
is det.
print_cf(Digit_list, !IO) :-
print_cf_loop(Digit_list, "[", !IO).
print_cf_loop(Digit_list, Sep, !IO) :-
(if (Digit_list = [Digit | More_digits])
then (print(Sep, !IO),
print(Digit, !IO),
(if (Sep = "[")
then print_cf_loop(More_digits, "; ", !IO)
else print_cf_loop(More_digits, ", ", !IO)))
else print("]", !IO)).
 
%%%-------------------------------------------------------------------
%%%
%%% ‘example’ takes numerator and denominator of a rational number,
%%% and prints a line of output.
%%%
 
:- pred example(int::in, int::in, io::di, io::uo) is det.
example(N1, N2, !IO) :-
print(N1, !IO),
print("/", !IO),
print(N2, !IO),
print(" => ", !IO),
print_cf(r2cf_digits(N1, N2), !IO),
nl(!IO).
 
%%%-------------------------------------------------------------------
 
main(!IO) :-
example(1, 2, !IO),
example(3, 1, !IO),
example(23, 8, !IO),
example(13, 11, !IO),
example(22, 7, !IO),
example(-151, 77, !IO),
example(14142, 10000, !IO),
example(141421, 100000, !IO),
example(1414214, 1000000, !IO),
example(14142136, 10000000, !IO),
example(31, 10, !IO),
example(314, 100, !IO),
example(3142, 1000, !IO),
example(31428, 10000, !IO),
example(314285, 100000, !IO),
example(3142857, 1000000, !IO),
example(31428571, 10000000, !IO),
example(314285714, 100000000, !IO),
true.
 
%%%-------------------------------------------------------------------
%%% local variables:
%%% mode: mercury
%%% prolog-indent-width: 2
%%% end:</syntaxhighlight>
 
{{out}}
 
<pre>$ mmc continued_fraction_from_rational.m && ./continued_fraction_from_rational
1/2 => [0; 2]
3/1 => [3]
23/8 => [2; 1, 7]
13/11 => [1; 5, 2]
22/7 => [3; 7]
-151/77 => [-2; 25, 1, 2]
14142/10000 => [1; 2, 2, 2, 2, 2, 1, 1, 29]
141421/100000 => [1; 2, 2, 2, 2, 2, 2, 3, 1, 1, 3, 1, 7, 2]
1414214/1000000 => [1; 2, 2, 2, 2, 2, 2, 2, 3, 6, 1, 2, 1, 12]
14142136/10000000 => [1; 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 1, 2, 4, 1, 1, 2]
31/10 => [3; 10]
314/100 => [3; 7, 7]
3142/1000 => [3; 7, 23, 1, 2]
31428/10000 => [3; 7, 357]
314285/100000 => [3; 7, 2857]
3142857/1000000 => [3; 7, 142857]
31428571/10000000 => [3; 7, 476190, 3]
314285714/100000000 => [3; 7, 7142857]</pre>
 
===An implementation using lazy lists===
{{trans|Haskell}}
{{works with|Mercury|22.01.1}}
 
This version has the advantage that it memoizes terms in a form that is efficient for sequential access.
 
I used arbitrary-precision numbers so I could plug in some big numbers.
 
''Important'': in Mercury, '''delay''' takes an explicit thunk (not an expression implicitly wrapped in a thunk) as its argument. If you use '''val''' instead of '''delay''', you will get eager evaluation.
 
<syntaxhighlight lang="mercury">
%%%-------------------------------------------------------------------
 
:- module continued_fraction_from_rational_lazy.
 
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
 
:- implementation.
:- import_module exception.
:- import_module integer. % Arbitrary-precision integers.
:- import_module lazy. % Lazy evaluation.
:- import_module list.
:- import_module string.
 
%% NOTE: There IS a "rational" module, for arbitrary-precision
%% rational numbers, but I wrote this example for plain "integer"
%% type. One could easily add "rational" support.
 
%%%-------------------------------------------------------------------
%%%
%%% The following lazy list implementation is suggested in the Mercury
%%% Library Reference.
%%%
 
:- type lazy_list(T)
---> lazy_list(lazy(list_cell(T))).
 
:- type list_cell(T)
---> cons(T, lazy_list(T))
; nil.
 
:- type cf == lazy_list(integer).
 
%%%-------------------------------------------------------------------
%%%
%%% r2cf takes numerator and denominator of a fraction, and returns a
%%% continued fraction as a lazy list of terms.
%%%
 
:- func r2cf(integer, integer) = cf.
r2cf(Numerator, Denominator) = CF :-
(if (Denominator = zero)
then (CF = lazy_list(delay((func) = nil)))
else (CF = lazy_list(delay(Cons)),
((func) = Cell :-
(Cell = cons(Quotient, r2cf(Denominator, Remainder)),
%% What follows is division with truncation towards zero.
divide_with_rem(Numerator, Denominator,
Quotient, Remainder))) = Cons)).
 
%%%-------------------------------------------------------------------
%%%
%%% cf2string and cf2string_with_max_terms convert a continued
%%% fraction to a printable string.
%%%
 
:- func cf2string(cf) = string.
cf2string(CF) = cf2string_with_max_terms(CF, integer(1000)).
 
:- func cf2string_with_max_terms(cf, integer) = string.
cf2string_with_max_terms(CF, MaxTerms) = S :-
S = cf2string_loop(CF, MaxTerms, zero, "[").
 
:- func cf2string_loop(cf, integer, integer, string) = string.
cf2string_loop(CF, MaxTerms, I, Accum) = S :-
(CF = lazy_list(ValCell),
force(ValCell) = Cell,
(if (Cell = cons(Term, Tail))
then (if (I = MaxTerms) then (S = Accum ++ ",...]")
else ((Separator = (if (I = zero) then ""
else if (I = one) then ";"
else ",")),
TermStr = to_string(Term),
S = cf2string_loop(Tail, MaxTerms, I + one,
Accum ++ Separator ++ TermStr)))
else (S = Accum ++ "]"))).
 
%%%-------------------------------------------------------------------
%%%
%%% example takes a fraction, as a string, or as separate numerator
%%% and denominator strings, and prints a line of output.
%%%
 
:- pred example(string::in, io::di, io::uo) is det.
:- pred example(string::in, string::in, io::di, io::uo) is det.
example(Fraction, !IO) :-
split_at_char(('/'), Fraction) = Split,
(if (Split = [Numerator])
then example_(Fraction, Numerator, "1", !IO)
else if (Split = [Numerator, Denominator])
then example_(Fraction, Numerator, Denominator, !IO)
else throw("Not an integer or fraction: \"" ++ Fraction ++ "\"")).
example(Numerator, Denominator, !IO) :-
example_(Numerator ++ "/" ++ Denominator,
Numerator, Denominator, !IO).
 
:- pred example_(string::in, string::in, string::in, io::di, io::uo)
is det.
example_(Fraction, Numerator, Denominator, !IO) :-
(N = integer.det_from_string(Numerator)),
(D = integer.det_from_string(Denominator)),
print(Fraction, !IO),
print(" => ", !IO),
print(cf2string(r2cf(N, D)), !IO),
nl(!IO).
 
%%%-------------------------------------------------------------------
 
main(!IO) :-
example("1/2", !IO),
example("3", !IO),
example("23/8", !IO),
example("13/11", !IO),
example("22/7", !IO),
example("-151/77", !IO),
 
%% I made "example" overloaded, so it can take separate numerator
%% and denominator strings.
example("151", "-77", !IO),
 
example("14142/10000", !IO),
example("141421/100000", !IO),
example("1414214/1000000", !IO),
example("14142136/10000000", !IO),
 
%% Decimal expansion of sqrt(2): see https://oeis.org/A002193
example("141421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157/100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", !IO),
 
example("31/10", !IO),
example("314/100", !IO),
example("3142/1000", !IO),
example("31428/10000", !IO),
example("314285/100000", !IO),
example("3142857/1000000", !IO),
example("31428571/10000000", !IO),
example("314285714/100000000", !IO),
 
%% Decimal expansion of pi: see https://oeis.org/A000796
example("314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214/100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", !IO),
 
true.
 
%%%-------------------------------------------------------------------
%%% local variables:
%%% mode: mercury
%%% prolog-indent-width: 2
%%% end:
</syntaxhighlight>
 
{{out}}
<pre>$ mmc --use-subdirs continued_fraction_from_rational_lazy.m && ./continued_fraction_from_rational_lazy
1/2 => [0;2]
3 => [3]
23/8 => [2;1,7]
13/11 => [1;5,2]
22/7 => [3;7]
-151/77 => [-1;-1,-24,-1,-2]
151/-77 => [-1;-1,-24,-1,-2]
14142/10000 => [1;2,2,2,2,2,1,1,29]
141421/100000 => [1;2,2,2,2,2,2,3,1,1,3,1,7,2]
1414214/1000000 => [1;2,2,2,2,2,2,2,3,6,1,2,1,12]
14142136/10000000 => [1;2,2,2,2,2,2,2,2,2,6,1,2,4,1,1,2]
141421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157/100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 => [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,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,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,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,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,5,1,8,3,1,2,2,1,6,2,2,3,1,2,3,1,1,39,1,3,1,2,4,10,1,6,1,30,5,2,1,1,1,1,1,32,1,4,18,124,3,2,1,1,8,1,1,1,6,15,2,3,2,7,1,4,9,2,7,1,1,1,1,1,2,1,10,1,31,5,1,1,1,7,1,14,10,3,11,1,2,1,65,4,9,2,3,2,2,9,1,1,2,1,1,2]
31/10 => [3;10]
314/100 => [3;7,7]
3142/1000 => [3;7,23,1,2]
31428/10000 => [3;7,357]
314285/100000 => [3;7,2857]
3142857/1000000 => [3;7,142857]
31428571/10000000 => [3;7,476190,3]
314285714/100000000 => [3;7,7142857]
314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214/100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 => [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,3,13,1,4,2,6,6,99,1,2,2,6,3,5,1,1,6,8,1,7,1,2,3,7,1,2,1,1,12,1,1,1,3,1,1,8,1,1,2,1,6,1,1,5,2,2,3,1,2,4,4,16,1,161,45,1,22,1,2,2,1,4,1,2,24,1,2,1,3,1,2,1,1,10,2,8,2,1,4,1,1,2,3,6,8,1,1,1,7,1,1,1,1,21,1,2,1,2,1,1,21,1,6,1,2,2,1,1,2,5,2,3,2,9,3,3,2,2,1,1,3,7,3,1,8,36,20,6,17,15,1,2,5,1,4,9,6,26,1,1,1,7,1,79,4,1,1,10,4,5,1,1,55,8,1,4,4,10,5,3,1,2,6,1,2,1,1,2,1,20,3,1,1,2,3]</pre>
 
=={{header|Modula-2}}==
<syntaxhighlight lang="modula2">MODULE ConstructFromrationalNumber;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
 
TYPE R2cf = RECORD
num,den : INTEGER;
END;
 
PROCEDURE HasNext(self : R2cf) : BOOLEAN;
BEGIN
RETURN self.den # 0;
END HasNext;
 
PROCEDURE Next(VAR self : R2cf) : INTEGER;
VAR div,rem : INTEGER;
BEGIN
div := self.num / self.den;
rem := self.num REM self.den;
self.num := self.den;
self.den := rem;
RETURN div;
END Next;
 
PROCEDURE Iterate(self : R2cf);
VAR buf : ARRAY[0..64] OF CHAR;
BEGIN
WHILE HasNext(self) DO
FormatString("%i ", buf, Next(self));
WriteString(buf);
END;
WriteLn;
END Iterate;
 
PROCEDURE Print(num,den : INTEGER);
VAR frac : R2cf;
VAR buf : ARRAY[0..64] OF CHAR;
BEGIN
FormatString("%9i / %-9i = ", buf, num, den);
WriteString(buf);
 
frac.num := num;
frac.den := den;
Iterate(frac);
END Print;
 
VAR frac : R2cf;
BEGIN
Print(1,2);
Print(3,1);
Print(23,8);
Print(13,11);
Print(22,7);
Print(-151,77);
 
WriteLn;
WriteString("Sqrt(2) ->");
WriteLn;
Print(14142,10000);
Print(141421,100000);
Print(1414214,1000000);
Print(14142136,10000000);
 
WriteLn;
WriteString("Pi ->");
WriteLn;
Print(31,10);
Print(314,100);
Print(3142,1000);
Print(31428,10000);
Print(314285,100000);
Print(3142857,1000000);
Print(31428571,10000000);
Print(314285714,100000000);
 
ReadChar;
END ConstructFromrationalNumber.</syntaxhighlight>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">iterator r2cf*(n1, n2: int): int =
var (n1, n2) = (n1, n2)
while n2 != 0:
yield n1 div n2
n1 = n1 mod n2
swap n1, n2
 
#———————————————————————————————————————————————————————————————————————————————————————————————————
 
when isMainModule:
 
from sequtils import toSeq
 
for pair in [(1, 2), (3, 1), (23, 8), (13, 11), (22, 7), (-151, 77)]:
echo pair, " -> ", toSeq(r2cf(pair[0], pair[1]))
 
echo ""
for pair in [(14142, 10000), (141421, 100000), (1414214, 1000000), (14142136, 10000000)]:
echo pair, " -> ", toSeq(r2cf(pair[0], pair[1]))
 
echo ""
for pair in [(31,10), (314,100), (3142,1000), (31428,10000), (314285,100000),
(3142857,1000000), (31428571,10000000), (314285714,100000000)]:
echo pair, " -> ", toSeq(r2cf(pair[0], pair[1]))</syntaxhighlight>
 
{{out}}
<pre>(1, 2) -> @[0, 2]
(3, 1) -> @[3]
(23, 8) -> @[2, 1, 7]
(13, 11) -> @[1, 5, 2]
(22, 7) -> @[3, 7]
(-151, 77) -> @[-1, -1, -24, -1, -2]
 
(14142, 10000) -> @[1, 2, 2, 2, 2, 2, 1, 1, 29]
(141421, 100000) -> @[1, 2, 2, 2, 2, 2, 2, 3, 1, 1, 3, 1, 7, 2]
(1414214, 1000000) -> @[1, 2, 2, 2, 2, 2, 2, 2, 3, 6, 1, 2, 1, 12]
(14142136, 10000000) -> @[1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 1, 2, 4, 1, 1, 2]
 
(31, 10) -> @[3, 10]
(314, 100) -> @[3, 7, 7]
(3142, 1000) -> @[3, 7, 23, 1, 2]
(31428, 10000) -> @[3, 7, 357]
(314285, 100000) -> @[3, 7, 2857]
(3142857, 1000000) -> @[3, 7, 142857]
(31428571, 10000000) -> @[3, 7, 476190, 3]
(314285714, 100000000) -> @[3, 7, 7142857]</pre>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">apply(contfrac,[1/2,3,23/8,13/11,22/7,-151/77])</langsyntaxhighlight>
{{out}}
<pre>[[0, 2], [3], [2, 1, 7], [1, 5, 2], [3, 7], [-2, 25, 1, 2]]</pre>
Line 660 ⟶ 3,812:
=={{header|Perl}}==
To do output one digit at a time, we first turn off buffering to be pedantic, then use a closure that yields one term per call.
<langsyntaxhighlight lang="perl">$|=1;
 
sub rc2f {
Line 685 ⟶ 3,837:
for ([14142,10000],[141421,100000],[1414214,1000000],[14142136,10000000]);
print "\n";
rcshow(rc2f(314285714,100000000));</langsyntaxhighlight>
{{out}}
<pre>
Line 703 ⟶ 3,855:
</pre>
 
=={{header|Perl 6Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
Straightforward implementation:
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<lang perl6>sub r2cf(Rat $x is copy) {
<span style="color: #008080;">function</span> <span style="color: #000000;">r2cf</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">num</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">denom</span><span style="color: #0000FF;">)</span>
gather loop {
<span style="color: #004080;">atom</span> <span style="color: #000000;">quot</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
$x -= take $x.floor;
<span style="color: #008080;">if</span> <span style="color: #000000;">denom</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
last unless $x > 0;
<span style="color: #000000;">quot</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">trunc</span><span style="color: #0000FF;">(</span><span style="color: #000000;">num</span><span style="color: #0000FF;">/</span><span style="color: #000000;">denom</span><span style="color: #0000FF;">)</span>
$x = 1 / $x;
<span style="color: #0000FF;">{</span><span style="color: #000000;">num</span><span style="color: #0000FF;">,</span><span style="color: #000000;">denom</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">denom</span><span style="color: #0000FF;">,</span><span style="color: #000000;">num</span><span style="color: #0000FF;">-</span><span style="color: #000000;">quot</span><span style="color: #0000FF;">*</span><span style="color: #000000;">denom</span><span style="color: #0000FF;">}</span>
}
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
}
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">quot</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">num</span><span style="color: #0000FF;">,</span><span style="color: #000000;">denom</span><span style="color: #0000FF;">}}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">txt</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">tests</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Running %s :\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">txt</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">atom</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">num</span><span style="color: #0000FF;">,</span><span style="color: #000000;">denom</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">quot</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">sep</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">";"</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d/%d ==&gt; ["</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">num</span><span style="color: #0000FF;">,</span><span style="color: #000000;">denom</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">denom</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">do</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">quot</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">num</span><span style="color: #0000FF;">,</span><span style="color: #000000;">denom</span><span style="color: #0000FF;">}}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">r2cf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">num</span><span style="color: #0000FF;">,</span><span style="color: #000000;">denom</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">quot</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">denom</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sep</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">sep</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">","</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"]\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">examples</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">23</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">13</span><span style="color: #0000FF;">,</span><span style="color: #000000;">11</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">22</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{-</span><span style="color: #000000;">151</span><span style="color: #0000FF;">,</span><span style="color: #000000;">77</span><span style="color: #0000FF;">}},</span>
<span style="color: #000000;">sqrt2</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">14142</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10000</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">141421</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100000</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1414214</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1000000</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">14142136</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10000000</span><span style="color: #0000FF;">}},</span>
<span style="color: #000000;">pi</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">31</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">314</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">3142</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">31428</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10000</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">314285</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100000</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">3142857</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1000000</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">31428571</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10000000</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">314285714</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100000000</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">3141592653589793</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1000000000000000</span><span style="color: #0000FF;">}}</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"the examples"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">examples</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"for sqrt(2)"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sqrt2</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"for pi"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pi</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Running the examples :
1/2 ==> [0;2]
3/1 ==> [3]
23/8 ==> [2;1,7]
13/11 ==> [1;5,2]
22/7 ==> [3;7]
-151/77 ==> [-1;-1,-24,-1,-2]
 
Running for sqrt(2) :
say r2cf(.Rat) for <1/2 3 23/8 13/11 22/7 1.41 1.4142136>;</lang>
14142/10000 ==> [1;2,2,2,2,2,1,1,29]
{{out}}
141421/100000 ==> [1;2,2,2,2,2,2,3,1,1,3,1,7,2]
<pre>(0 2)
1414214/1000000 ==> [1;2,2,2,2,2,2,2,3,6,1,2,1,12]
(3)
14142136/10000000 ==> [1;2,2,2,2,2,2,2,2,2,6,1,2,4,1,1,2]
(2 1 7)
 
(1 5 2)
Running for pi :
(3 7)
31/10 ==> [3;10]
(1 2 2 3 1 1 2)
314/100 ==> [3;7,7]
(1 2 2 2 2 2 2 2 2 2 6 1 2 4 1 1 2)</pre>
3142/1000 ==> [3;7,23,1,2]
As a silly one-liner:
31428/10000 ==> [3;7,357]
<lang perl6>sub r2cf(Rat $x is copy) { gather $x [R/]= 1 while ($x -= take $x.floor) > 0 }</lang>
314285/100000 ==> [3;7,2857]
3142857/1000000 ==> [3;7,142857]
31428571/10000000 ==> [3;7,476190,3]
314285714/100000000 ==> [3;7,7142857]
3141592653589793/1000000000000000 ==> [3;7,15,1,292,1,1,1,2,1,3,1,14,4,2,3,1,12,5,1,5,20,1,11,1,1,1,2]
</pre>
 
=={{header|Python}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="python">def r2cf(n1,n2):
while n2:
n1, (t1, n2) = n2, divmod(n1, n2)
Line 740 ⟶ 3,937:
print(list(r2cf(141421,100000))) # => [1, 2, 2, 2, 2, 2, 2, 3, 1, 1, 3, 1, 7, 2]
print(list(r2cf(1414214,1000000))) # => [1, 2, 2, 2, 2, 2, 2, 2, 3, 6, 1, 2, 1, 12]
print(list(r2cf(14142136,10000000))) # => [1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 1, 2, 4, 1, 1, 2]</langsyntaxhighlight>
This version generates it from any real number (with rationals as a special case):
<langsyntaxhighlight lang="python">def real2cf(x):
while True:
t1, f = divmod(x, 1)
Line 754 ⟶ 3,951:
 
print(list(real2cf(Fraction(13, 11)))) # => [1, 5, 2]
print(list(islice(real2cf(2 ** 0.5), 20))) # => [1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]</langsyntaxhighlight>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="quackery">
[ $ "bigrat.qky" loadfile ] now!
 
[ [] unrot
[ proper
2swap join unrot
over 0 != while
1/v again ]
2drop ] is cf ( n/d --> [ )
 
' [ [ 1 2 ]
[ 3 1 ]
[ 23 8 ]
[ 13 11 ]
[ 22 7 ]
[ -151 77 ]
[ 14142 10000 ]
[ 141421 100000 ]
[ 1414214 1000000 ]
[ 14142136 10000000 ]
[ 31 10 ]
[ 314 100 ]
[ 3142 1000 ]
[ 31428 10000 ]
[ 314285 100000 ]
[ 3142857 1000000 ]
[ 31428571 10000000 ]
[ 314285714 100000000 ] ]
 
witheach
[ do over echo say "/"
dup echo
say " = "
cf echo cr ]</syntaxhighlight>
 
{{out}}
 
<pre>1/2 = [ 0 2 ]
3/1 = [ 3 ]
23/8 = [ 2 1 7 ]
13/11 = [ 1 5 2 ]
22/7 = [ 3 7 ]
-151/77 = [ -2 25 1 2 ]
14142/10000 = [ 1 2 2 2 2 2 1 1 29 ]
141421/100000 = [ 1 2 2 2 2 2 2 3 1 1 3 1 7 2 ]
1414214/1000000 = [ 1 2 2 2 2 2 2 2 3 6 1 2 1 12 ]
14142136/10000000 = [ 1 2 2 2 2 2 2 2 2 2 6 1 2 4 1 1 2 ]
31/10 = [ 3 10 ]
314/100 = [ 3 7 7 ]
3142/1000 = [ 3 7 23 1 2 ]
31428/10000 = [ 3 7 357 ]
314285/100000 = [ 3 7 2857 ]
3142857/1000000 = [ 3 7 142857 ]
31428571/10000000 = [ 3 7 476190 3 ]
314285714/100000000 = [ 3 7 7142857 ]
</pre>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
 
Line 780 ⟶ 4,036:
(real->cf (sqrt 2) 10)
(real->cf pi 10)
</syntaxhighlight>
</lang>
 
{{out}}
Line 788 ⟶ 4,044:
'(1 2 2 2 2 2 2 2 2 2 2 2 2 3 3 1 3 8 9 1 20 1 2)
'(3 7 15 1 292 1 1 6 2 13 3 1 12 3)
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
Straightforward implementation:
<syntaxhighlight lang="raku" line>sub r2cf(Rat $x is copy) {
gather loop {
$x -= take $x.floor;
last unless $x > 0;
$x = 1 / $x;
}
}
 
say r2cf(.Rat) for <1/2 3 23/8 13/11 22/7 1.41 1.4142136>;</syntaxhighlight>
{{out}}
<pre>(0 2)
(3)
(2 1 7)
(1 5 2)
(3 7)
(1 2 2 3 1 1 2)
(1 2 2 2 2 2 2 2 2 2 6 1 2 4 1 1 2)</pre>
As a silly one-liner:
<syntaxhighlight lang="raku" line>sub r2cf(Rat $x is copy) { gather $x [R/]= 1 while ($x -= take $x.floor) > 0 }</syntaxhighlight>
 
=={{header|RATFOR}}==
{{works with|ratfor77|[https://sourceforge.net/p/chemoelectric/ratfor77/ public domain 1.0]}}
{{works with|gfortran|12.2.1}}
 
To get nice output with f2c would have been more tedious than I felt like doing, and so the I/O facilities employed here are more advanced than often was the case with an historical FORTRAN77 compiler.
 
<syntaxhighlight lang="ratfor">
# This implementation assumes the I/O facilities of gfortran, and so
# is not suited to f2c as the FORTRAN77 compiler.
 
function r2cf (N1, N2)
implicit none
 
integer N1, N2
integer r2cf
 
integer r
 
# We will use division with rounding towards zero, which is the
# native integer division method of FORTRAN77.
r2cf = N1 / N2
r = mod (N1, N2)
 
N1 = N2
N2 = r
end
 
subroutine wrr2cf (N1, N2) # Write r2cf results.
implicit none
 
integer N1, N2
integer r2cf
integer digit, M1, M2
integer sep
 
write (*, '(I0, "/", I0, " => ")', advance = "no") N1, N2
 
M1 = N1
M2 = N2
sep = 0
while (M2 != 0)
{
digit = r2cf (M1, M2)
if (sep == 0)
{
write (*, '("[", I0)', advance = "no") digit
sep = 1
}
else if (sep == 1)
{
write (*, '("; ", I0)', advance = "no") digit
sep = 2
}
else
{
write (*, '(", ", I0)', advance = "no") digit
}
}
write (*, '("]")', advance = "yes")
end
 
program demo
implicit none
 
call wrr2cf (1, 2)
call wrr2cf (3, 1)
call wrr2cf (23, 8)
call wrr2cf (13, 11)
call wrr2cf (22, 7)
call wrr2cf (-151, 77)
 
call wrr2cf (14142, 10000)
call wrr2cf (141421, 100000)
call wrr2cf (1414214, 1000000)
call wrr2cf (14142136, 10000000)
 
call wrr2cf (31, 10)
call wrr2cf (314, 100)
call wrr2cf (3142, 1000)
call wrr2cf (31428, 10000)
call wrr2cf (314285, 100000)
call wrr2cf (3142857, 1000000)
call wrr2cf (31428571, 10000000)
call wrr2cf (314285714, 100000000)
end
</syntaxhighlight>
 
{{out}}
 
<pre>$ (ratfor77 continued-fraction-from-rational.r | gfortran -x f77 -) && ./a.out
1/2 => [0; 2]
3/1 => [3]
23/8 => [2; 1, 7]
13/11 => [1; 5, 2]
22/7 => [3; 7]
-151/77 => [-1; -1, -24, -1, -2]
14142/10000 => [1; 2, 2, 2, 2, 2, 1, 1, 29]
141421/100000 => [1; 2, 2, 2, 2, 2, 2, 3, 1, 1, 3, 1, 7, 2]
1414214/1000000 => [1; 2, 2, 2, 2, 2, 2, 2, 3, 6, 1, 2, 1, 12]
14142136/10000000 => [1; 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 1, 2, 4, 1, 1, 2]
31/10 => [3; 10]
314/100 => [3; 7, 7]
3142/1000 => [3; 7, 23, 1, 2]
31428/10000 => [3; 7, 357]
314285/100000 => [3; 7, 2857]
3142857/1000000 => [3; 7, 142857]
31428571/10000000 => [3; 7, 476190, 3]
314285714/100000000 => [3; 7, 7142857]
</pre>
 
=={{header|REXX}}==
Programming notes:
* &nbsp; IncreaseIncreasing <code>&nbsp; '''numeric digits</code>''' &nbsp; to a higher value towill generate more terms.
* &nbsp; Two subroutines, <code>&nbsp; '''sqrt</code>''' &ampnbsp; <code>and &nbsp; '''pi</code>''', &nbsp; were included here to demonstrate terms for &nbsp; √<span style="text-decoration: overline"> 2 </span> &nbsp; and π&nbsp; pi.
* &nbsp; The subroutine <code>&nbsp; '''$maxfact</code>''' &nbsp; was included and is only needed if the number used for <code>&nbsp; '''r2cf</code>''' &nbsp; is a decimal fraction.
* &nbsp; Checks were included to verify that the arguments being passed to <code>&nbsp; '''r2cf</code>''' &nbsp; are indeed numeric and also not zero.
* &nbsp; This REXX version also handles negative numbers.
<langsyntaxhighlight lang="rexx">/*REXX pgmprogram converts a decimal or rational fraction to a continued fraction. */
numeric digits 230 /*this determines how many terms to be gened*/
say ' 1/2 ──► CF: ' r2cf( '1/2' /*can be generated for dec fracts*/)
say ' 1/2 3 ──► CF: ' r2cf( '1/2' 3 )
say ' 3 23/8 ──► CF: ' r2cf( 3'23/8' )
say ' 2313/8 11 ──► CF: ' r2cf( '2313/811' )
say ' 1322/117 ──► CF: ' r2cf( '1322/117 ' )
say ' 22/7 ──► CF: ' r2cf( '22/7 ___' )
say '───────── attempts at √ 2.'
say
say '14142/1e4 ──► CF: ' r2cf( '14142/1e4 ' )
say '───────── attempts at √2.'
say '14142141421/1e4 1e5 ──► CF: ' r2cf( '14142141421/1e41e5 ' )
say '1414211414214/1e5 1e6 ──► CF: ' r2cf( '1414211414214/1e51e6 ' )
say '141421414142136/1e6 1e7 ──► CF: ' r2cf( '141421414142136/1e61e7 ' )
say '14142136141421356/1e7 1e8 ──► CF: ' r2cf( '14142136141421356/1e71e8 ' )
say '1414213561414213562/1e8 1e9 ──► CF: ' r2cf( '1414213561414213562/1e81e9 ' )
say '141421356214142135624/1e9 1e10 ──► CF: ' r2cf( '141421356214142135624/1e91e10 ' )
say '14142135624141421356237/1e10 1e11 ──► CF: ' r2cf( '14142135624141421356237/1e101e11 ' )
say '141421356237/1e11 ──► CF: ' r2cf( '141421356237/1e11 ' )
say '1414213562373/1e12 ──► CF: ' r2cf( '1414213562373/1e12 ' )
say '√2 ──► CF: ' r2cf( sqrt(2) )
say
say '───────── an attempt at πpi'
say 'π pi ──► CF: ' r2cf( pi() )
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*────────────────────────────────R2CF subroutine───────────────────────*/
r2cf$maxFact: procedure; parse arg gx 1 s_x,y; 2 y=10**(digits()-1); $ b=0; h=1; if sa=='-'1; then g = substr(g,2)=0
do while a<=y & g<=y; n=trunc(_x); _=a; a=n*a+b; b=_; _=g; g=n*g+h; else s h=_
if n=_x | a/g=x then do; if a>y | g>y then iterate; b=a; h=g; leave; end
if pos('.',g)\==0 then do
_x=1/(_x-n); end; if \datatype(g,'N') then call serr 'not numeric:' g return b'/'h
/*──────────────────────────────────────────────────────────────────────────────────────*/
g = $maxfact(g)
pi: return 3.1415926535897932384626433832795028841971693993751058209749445923078164062862,
end
|| 089986280348253421170679821480865132823066470938446095505822317253594081284,
if pos('/',g)==0 then g = g"/"1
|| 811174502841027019385211055596446229489549303819644288109756659334461284756,
parse var g n '/' d
|| 48233786783165271 /* ··· should ≥ NUMERIC DIGITS */
if \datatype(n,'W') then call serr "a numerator isn't an integer:" n
/*──────────────────────────────────────────────────────────────────────────────────────*/
if \datatype(d,'W') then call serr "a denominator isn't an integer:" d
r2cf: procedure; parse arg g 1 s 2; $=; if s=='-' then g=substr(g, 2)
n = abs(n) /*ensure numerator is positive. */
if d=0 then call serr 'a denominator is zero' else s=
if pos(., g)\==0 then do; if \datatype(g, 'N') then call serr 'not numeric:' g
 
do while d\==0 /*where the rubber meets the road*/ g=$maxfact(g)
$ = $ s || (n%d) /*append another number to list. */end
if pos('/', g)==0 then _ g= dg"/"1
parse var g d = n // d '/*' % is int div, // is modulus.*/d
if \datatype(n, 'W') then call serr n"a =numerator _isn't an integer:" n
if \datatype(d, 'W') then call serr "a denominator endisn't an integer:" /*while*/ d
if d=0 then call serr 'a denominator is zero'
return strip($)
n=abs(n) /*ensure numerator is positive. */
/*─────────────────────────────PI subroutine────────────────────────────*/
pi: return, do while /*a bit of overkill, but hey !! */ d\==0; _=d /*where ··· should ≥the rubber NUMERICmeets DIGITSthe road*/
$=$ s || (n%d) /*append another number to list. */
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271
d=n // d; n=_ /* % is int div, // is modulus.*/
/*─────────────────────────────SERR subroutine──────────────────────────*/
serr: say; say '***error!***'; say; say arg(1); say; exitend /*while*/
return strip($)
/*─────────────────────────────SQRT subroutine──────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); i=; m.=9
serr: say; say numeric'***error***'; digits 9; numeric formsay; h=d+6; if x<0say arg(1); then do; x=-x; i='i'say; end exit 13
/*──────────────────────────────────────────────────────────────────────────────────────*/
parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g*.5'e'_%2
sqrt: procedure; parse arg x; if do jx=0 whilethen h>9return 0; m.jd=hdigits(); h=h%2d+16; numeric end /*j*/form
m.=9; numeric digits; doparse k=j+5 tovalue format(x,2,1,,0) 'E0' bywith -1;g 'E' numeric_ digits m.k; g=(g+x/g)*.5; end /*k*/'e'_%2
numeric digits d do j=0 while h>9; return (g/1)im.j=h; /*make complex if h=h%2+1; X < 0. end /*j*/
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/
/*─────────────────────────────MAXFACT subroutine───────────────────────*/
numeric digits d; return g/1</syntaxhighlight>
$maxFact: procedure; parse arg x 1 _x,y; y=10**(digits()-1); b=0; h=1
{{out|output|text=&nbsp; when using the default (internal) inputs:}}
a=1; g=0; do while a<=y & g<=y; n=trunc(_x); _=a; a=n*a+b; b=_; _=g
<pre>
g=n*g+h; h=_; if n=_x | a/g=x then do; if a>y | g>y then iterate; b=a
h=g; leave; end; _x=1/(_x-n); end; return b'/'h</lang>
{{out}}
<pre style="overflow:scroll">
1/2 ──► CF: 0 2
3 ──► CF: 3
Line 866 ⟶ 4,251:
13/11 ──► CF: 1 5 2
22/7 ──► CF: 3 7
___
 
───────── attempts at √2√ 2.
14142/1e4 ──► CF: 1 2 2 2 2 2 1 1 29
141421/1e5 ──► CF: 1 2 2 2 2 2 2 3 1 1 3 1 7 2
Line 879 ⟶ 4,264:
√2 ──► CF: 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 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 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 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 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 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 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 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 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 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 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3
 
───────── an attempt at πpi
π pi ──► CF: 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 3 13 1 4 2 6 6 99 1 2 2 6 3 5 1 1 6 8 1 7 1 2 3 7 1 2 1 1 12 1 1 1 3 1 1 8 1 1 2 1 6 1 1 5 2 2 3 1 2 4 4 16 1 161 45 1 22 1 2 2 1 4 1 2 24 1 2 1 3 1 2 1 1 10 2 5 4 1 2 2 8 1 5 2 2 26 1 4 1 1 8 2 42 2 1 7 3 3 1 1 7 2 4 9 7 2 3 1 57 1 18 1 9 19 1 2 18 1 3 7 30 1 1 1 3 3 3 1 2 8 1 1 2 1 15 1 2 13 1 2 1 4 1 12 1 1 3 3 28 1 10 3 2 20 1 1 1 1 4 1 1 1 5 3 2 1 6 1 4 1 120 2 1 1 3 1 23 1 15 1 3 7 1 16 1 2 1 21 2 1 1 2 9 1 6 4
</pre>
 
=={{header|RPL}}==
Half of the code is actually here to extract N1 and N2 from the expression 'N1/N2'. The algorithm itself is within the <code>WHILE..REPEAT..END</code> loop.
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! RPL code
! Comment
|-
|
DUP 1 EXGET EVAL SWAP
'''IF''' DUP SIZE 3 < '''THEN''' DROP 1
'''ELSE''' DUP SIZE EXGET '''END'''
{ } SWAP
'''WHILE''' DUP '''REPEAT'''
ROT OVER MOD LAST / FLOOR
4 ROLL SWAP + SWAP '''END'''
ROT DROP2 LIST→ →ARRY
≫ ‘RC2F’ STO
|
'''RC2F''' ''( 'n1/n2' - [ a0 a1.. an ] ) ''
get numerator
if no denominator, use 1
else get it
prepare stack 3:n1 2:output list 1:n2
loop
divmod(n1,n2)
add n1//n2 to list
clean stack, convert data type to have [] instead of {}
|}
{{in}}
<pre>
≪ { '1/2' '3' '23/8' '13/11' '22/7' '-151/77' '14142/10000' '141421/100000' '1414214/1000000' '14142136/10000000' '31/10' '314/100' '3142/1000' '31428/10000' '314285/100000' '3142857/1000000' '31428571/10000000' '314285714/100000000' } → fracs
≪ {} 1 fracs SIZE FOR j fracs j GET RC2F + NEXT ≫ ≫ EVAL
</pre>
{{out}}
<pre>
1: { [ 0 2 ] [ 3 ] [ 2 1 7 ] [ 1 5 2 ] [ 3 7 ] [ -2 25 1 2 ] [ 1 2 2 2 2 2 1 1 29 ] [ 1 2 2 2 2 2 2 3 1 1 3 1 7 2 ] [ 1 2 2 2 2 2 2 2 3 6 1 2 1 12 ] [ 1 2 2 2 2 2 2 2 2 2 6 1 2 4 1 1 2 ] [ 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 ] }
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby"># Generate a continued fraction from a rational number
 
def r2cf(n1,n2)
Line 891 ⟶ 4,316:
yield t1
end
end</langsyntaxhighlight>
===Testing===
'''Test 1:'''
<langsyntaxhighlight lang="ruby">[[1,2], [3,1], [23,8], [13,11], [22,7], [-151,77]].each do |n1,n2|
print "%10s : " % "#{n1} / #{n2}"
r2cf(n1,n2) {|n| print "#{n} "}
puts
end</langsyntaxhighlight>
{{out}}
<pre>
Line 910 ⟶ 4,335:
'''Test 2:'''
<math>\sqrt 2</math>
<langsyntaxhighlight lang="ruby">(5..8).each do |digit|
n2 = 10 ** (digit-1)
n1 = (Math.sqrt(2) * n2).round
Line 916 ⟶ 4,341:
r2cf(n1,n2) {|n| print "#{n} "}
puts
end</langsyntaxhighlight>
{{out}}
<pre>
Line 925 ⟶ 4,350:
</pre>
'''Test 3:'''
<langsyntaxhighlight lang="ruby">a =[ [31,10],
[314,100],
[3142,1000],
Line 938 ⟶ 4,363:
r2cf(n1,n2) {|n| print "#{n} "}
puts
end</langsyntaxhighlight>
{{out}}
<pre>
Line 949 ⟶ 4,374:
31428571 / 10000000 : 3 7 476190 3
314285714 / 100000000 : 3 7 7142857
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">
struct R2cf {
n1: i64,
n2: i64
}
 
// This iterator generates the continued fraction representation from the
// specified rational number.
impl Iterator for R2cf {
type Item = i64;
 
fn next(&mut self) -> Option<i64> {
if self.n2 == 0 {
None
}
else {
let t1 = self.n1 / self.n2;
let t2 = self.n2;
self.n2 = self.n1 - t1 * t2;
self.n1 = t2;
Some(t1)
}
}
}
 
fn r2cf(n1: i64, n2: i64) -> R2cf {
R2cf { n1: n1, n2: n2 }
}
 
macro_rules! printcf {
($x:expr, $y:expr) => (println!("{:?}", r2cf($x, $y).collect::<Vec<_>>()));
}
 
fn main() {
printcf!(1, 2);
printcf!(3, 1);
printcf!(23, 8);
printcf!(13, 11);
printcf!(22, 7);
printcf!(-152, 77);
 
printcf!(14_142, 10_000);
printcf!(141_421, 100_000);
printcf!(1_414_214, 1_000_000);
printcf!(14_142_136, 10_000_000);
 
printcf!(31, 10);
printcf!(314, 100);
printcf!(3142, 1000);
printcf!(31_428, 10_000);
printcf!(314_285, 100_000);
printcf!(3_142_857, 1_000_000);
printcf!(31_428_571, 10_000_000);
printcf!(314_285_714, 100_000_000);
}
</syntaxhighlight>
 
{{out}}
<pre>
[0, 2]
[3]
[2, 1, 7]
[1, 5, 2]
[3, 7]
[-1, -1, -37, -2]
[1, 2, 2, 2, 2, 2, 1, 1, 29]
[1, 2, 2, 2, 2, 2, 2, 3, 1, 1, 3, 1, 7, 2]
[1, 2, 2, 2, 2, 2, 2, 2, 3, 6, 1, 2, 1, 12]
[1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 1, 2, 4, 1, 1, 2]
[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]
</pre>
=={{header|Scheme}}==
===Using a closure to generate terms===
{{works with|Chez Scheme}}
'''The Implementation'''
<syntaxhighlight lang="scheme">; Create a terminating Continued Fraction generator for the given rational number.
; Returns one term per call; returns #f when no more terms remaining.
(define make-continued-fraction-gen
(lambda (rat)
(let ((num (numerator rat)) (den (denominator rat)))
(lambda ()
(if (= den 0)
#f
(let ((ret (quotient num den))
(rem (modulo num den)))
(set! num den)
(set! den rem)
ret))))))
 
; Return the continued fraction representation of a rational number as a string.
(define rat->cf-string
(lambda (rat)
(let* ((cf (make-continued-fraction-gen rat))
(str (string-append "[" (format "~a" (cf))))
(sep ";"))
(let loop ((term (cf)))
(when term
(set! str (string-append str (format "~a ~a" sep term)))
(set! sep ",")
(loop (cf))))
(string-append str "]"))))
 
; Return the continued fraction representation of a rational number as a list of terms.
(define rat->cf-list
(lambda (rat)
(let ((cf (make-continued-fraction-gen rat))
(lst '()))
(let loop ((term (cf)))
(when term
(set! lst (append lst (list term)))
(loop (cf))))
lst)))</syntaxhighlight>
'''The Task'''
<br />
Each continued fraction is displayed in both the conventional written form and as a list of terms.
<syntaxhighlight lang="scheme">(printf "~%Basic examples:~%")
(for-each
(lambda (rat)
(printf "~a = ~a~%" rat (rat->cf-string rat))
(printf "~a : ~a~%" rat (rat->cf-list rat)))
'(1/2 3 23/8 13/11 22/7 -151/77 0))
 
(printf "~%Root2 approximations:~%")
(for-each
(lambda (rat)
(printf "~a = ~a~%" rat (rat->cf-string rat))
(printf "~a : ~a~%" rat (rat->cf-list rat)))
'(14142/10000 141421/100000 1414214/1000000 14142136/10000000 141421356237/100000000000))
 
(printf "~%Pi approximations:~%")
(for-each
(lambda (rat)
(printf "~a = ~a~%" rat (rat->cf-string rat))
(printf "~a : ~a~%" rat (rat->cf-list rat)))
'(31/10 314/100 3142/1000 31428/10000 314285/100000 3142857/1000000
31428571/10000000 314285714/100000000 31415926535898/10000000000000))</syntaxhighlight>
{{out}}
<pre>
Basic examples:
1/2 = [0; 2]
1/2 : (0 2)
3 = [3]
3 : (3)
23/8 = [2; 1, 7]
23/8 : (2 1 7)
13/11 = [1; 5, 2]
13/11 : (1 5 2)
22/7 = [3; 7]
22/7 : (3 7)
-151/77 = [-1; 25, 1, 2]
-151/77 : (-1 25 1 2)
0 = [0]
0 : (0)
 
Root2 approximations:
7071/5000 = [1; 2, 2, 2, 2, 2, 1, 1, 29]
7071/5000 : (1 2 2 2 2 2 1 1 29)
141421/100000 = [1; 2, 2, 2, 2, 2, 2, 3, 1, 1, 3, 1, 7, 2]
141421/100000 : (1 2 2 2 2 2 2 3 1 1 3 1 7 2)
707107/500000 = [1; 2, 2, 2, 2, 2, 2, 2, 3, 6, 1, 2, 1, 12]
707107/500000 : (1 2 2 2 2 2 2 2 3 6 1 2 1 12)
1767767/1250000 = [1; 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 1, 2, 4, 1, 1, 2]
1767767/1250000 : (1 2 2 2 2 2 2 2 2 2 6 1 2 4 1 1 2)
141421356237/100000000000 = [1; 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 1, 4, 1, 2, 1, 63, 2, 1, 1, 1, 4, 2]
141421356237/100000000000 : (1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 1 4 1 2 1 63 2 1 1 1 4 2)
 
Pi approximations:
31/10 = [3; 10]
31/10 : (3 10)
157/50 = [3; 7, 7]
157/50 : (3 7 7)
1571/500 = [3; 7, 23, 1, 2]
1571/500 : (3 7 23 1 2)
7857/2500 = [3; 7, 357]
7857/2500 : (3 7 357)
62857/20000 = [3; 7, 2857]
62857/20000 : (3 7 2857)
3142857/1000000 = [3; 7, 142857]
3142857/1000000 : (3 7 142857)
31428571/10000000 = [3; 7, 476190, 3]
31428571/10000000 : (3 7 476190 3)
157142857/50000000 = [3; 7, 7142857]
157142857/50000000 : (3 7 7142857)
15707963267949/5000000000000 = [3; 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 21, 17, 1, 1, 1, 1, 8, 1, 7, 2, 1, 2, 2]
15707963267949/5000000000000 : (3 7 15 1 292 1 1 1 2 1 3 1 21 17 1 1 1 1 8 1 7 2 1 2 2)
</pre>
 
===Using SRFI-41 streams (lazy lists)===
{{trans|Haskell}}
{{works with|Gauche Scheme|0.9.12}}
{{works with|Chibi Scheme|0.10.0}}
{{works with|CHICKEN Scheme|5.3.0}}
 
This is for R7RS Scheme. Modify as necessary, for your Scheme. For CHICKEN, you will need the '''r7rs''' and '''srfi-41''' eggs.
 
Due to the use of a lazy list, the terms are memoized in a manner suitable for sequential access again and again.
 
(For -151/77 the solution here is for floor division. You will get a different solution if you round the fraction differently.)
 
<syntaxhighlight lang="scheme">
(cond-expand
(r7rs)
(chicken (import (r7rs))))
 
(import (scheme base))
(import (scheme case-lambda))
(import (scheme write))
(import (srfi 41))
 
;;;-------------------------------------------------------------------
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; The entirety of r2cf, two different ways ;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
;; r2cf-VERSION1 works with integers. (Any floating-point number is
;; first converted to exact rational.)
(define (r2cf-VERSION1 fraction)
(define-stream (recurs n d)
(if (zero? d)
stream-null
(let-values (((q r) (floor/ n d)))
(stream-cons q (recurs d r)))))
(let ((fraction (exact fraction)))
(recurs (numerator fraction) (denominator fraction))))
 
;; r2cf-VERSION2 works directly with fractions. (Any floating-point
;; number is first converted to exact rational.)
(define (r2cf-VERSION2 fraction)
(define-stream (recurs fraction)
(let* ((quotient (floor fraction))
(remainder (- fraction quotient)))
(stream-cons quotient (if (zero? remainder)
stream-null
(recurs (/ remainder))))))
(recurs (exact fraction)))
 
;;(define r2cf r2cf-VERSION1)
(define r2cf r2cf-VERSION2)
 
;;;-------------------------------------------------------------------
 
(define *max-terms* (make-parameter 20))
 
(define cf2string
(case-lambda
((cf) (cf2string cf (*max-terms*)))
((cf max-terms)
(let loop ((i 0)
(s "[")
(strm cf))
(if (stream-null? strm)
(string-append s "]")
(let ((term (stream-car strm))
(tail (stream-cdr strm)))
(if (= i max-terms)
(string-append s ",...]")
(let ((separator (case i
((0) "")
((1) ";")
(else ",")))
(term-str (number->string term)))
(loop (+ i 1)
(string-append s separator term-str)
tail)))))))))
 
(define (show fraction)
(parameterize ((*max-terms* 1000))
(display fraction)
(display " => ")
(display (cf2string (r2cf fraction)))
(newline)))
 
(show 1/2)
(show 3)
(show 23/8)
(show 13/11)
(show 22/11)
(show -151/77)
(show 14142/10000)
(show 141421/100000)
(show 1414214/1000000)
(show 14142136/10000000)
(show 1414213562373095049/1000000000000000000)
 
;; Decimal expansion of sqrt(2): see https://oeis.org/A002193
(show 141421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157/100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000)
 
(show 31/10)
(show 314/100)
(show 3142/1000)
(show 31428/10000)
(show 314285/100000)
(show 3142857/1000000)
(show 31428571/10000000)
(show 314285714/100000000)
(show 3142857142857143/1000000000000000)
 
;; Decimal expansion of pi: see https://oeis.org/A000796
(show 314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214/100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000)
</syntaxhighlight>
 
{{out}}
<pre>$ gosh continued-fraction-from-rational-srfi41.scm
1/2 => [0;2]
3 => [3]
23/8 => [2;1,7]
13/11 => [1;5,2]
2 => [2]
-151/77 => [-2;25,1,2]
7071/5000 => [1;2,2,2,2,2,1,1,29]
141421/100000 => [1;2,2,2,2,2,2,3,1,1,3,1,7,2]
707107/500000 => [1;2,2,2,2,2,2,2,3,6,1,2,1,12]
1767767/1250000 => [1;2,2,2,2,2,2,2,2,2,6,1,2,4,1,1,2]
1414213562373095049/1000000000000000000 => [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,1,39,1,5,1,3,61,1,1,8,1,2,1,7,1,1,6]
141421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157/100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 => [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,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,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,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,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,5,1,8,3,1,2,2,1,6,2,2,3,1,2,3,1,1,39,1,3,1,2,4,10,1,6,1,30,5,2,1,1,1,1,1,32,1,4,18,124,3,2,1,1,8,1,1,1,6,15,2,3,2,7,1,4,9,2,7,1,1,1,1,1,2,1,10,1,31,5,1,1,1,7,1,14,10,3,11,1,2,1,65,4,9,2,3,2,2,9,1,1,2,1,1,2]
31/10 => [3;10]
157/50 => [3;7,7]
1571/500 => [3;7,23,1,2]
7857/2500 => [3;7,357]
62857/20000 => [3;7,2857]
3142857/1000000 => [3;7,142857]
31428571/10000000 => [3;7,476190,3]
157142857/50000000 => [3;7,7142857]
3142857142857143/1000000000000000 => [3;6,1,142857142857142]
157079632679489661923132169163975144209858469968755291048747229615390820314310449931401741267105853399107/50000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 => [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,3,13,1,4,2,6,6,99,1,2,2,6,3,5,1,1,6,8,1,7,1,2,3,7,1,2,1,1,12,1,1,1,3,1,1,8,1,1,2,1,6,1,1,5,2,2,3,1,2,4,4,16,1,161,45,1,22,1,2,2,1,4,1,2,24,1,2,1,3,1,2,1,1,10,2,8,2,1,4,1,1,2,3,6,8,1,1,1,7,1,1,1,1,21,1,2,1,2,1,1,21,1,6,1,2,2,1,1,2,5,2,3,2,9,3,3,2,2,1,1,3,7,3,1,8,36,20,6,17,15,1,2,5,1,4,9,6,26,1,1,1,7,1,79,4,1,1,10,4,5,1,1,55,8,1,4,4,10,5,3,1,2,6,1,2,1,1,2,1,20,3,1,1,2,3]</pre>
 
===Using ''call-with-current-continuation'' to implement coroutines===
{{works with|Gauche Scheme|0.9.12}}
{{works with|Chibi Scheme|0.10.0}}
{{works with|CHICKEN Scheme|5.3.0}}
 
This is for R7RS Scheme. Modify as necessary, for your Scheme. For CHICKEN, you will need the '''r7rs''' egg.
 
This implementation employs coroutines. The '''r2cf''' procedure is passed not only a number to convert to a continued fraction, but also a "consumer" of the terms. In this case, the consumer is '''display-cf''', which prints the terms nicely.
 
The implementation here is, in a way, backwards from the requirements of the task: the producer is going first, so that the consumer does not have to "ask for" the first term. But I had not thought of that before writing the code, and also every term after the first can be thought of as "asked for".
 
(For -151/77 the solution here is for floor division. You will get a different solution if you round the fraction differently.)
 
<syntaxhighlight lang="scheme">
(cond-expand
(r7rs)
(chicken (import (r7rs))))
 
(import (scheme base))
(import (scheme write))
 
;;;-------------------------------------------------------------------
 
(define (r2cf fraction consumer)
(let* ((fraction (exact fraction)))
(let loop ((n (numerator fraction))
(d (denominator fraction))
(consumer consumer))
(if (zero? d)
(call-with-current-continuation
(lambda (kont) (consumer #f kont)))
(let-values (((q r) (floor/ n d)))
(loop d r (call-with-current-continuation
(lambda (kont) (consumer q kont)))))))))
 
(define (display-cf term producer)
(display "[")
(let loop ((term term)
(producer producer)
(separator ""))
(if term
(begin
(display separator)
(display term)
(let-values (((term producer)
(call-with-current-continuation producer)))
(loop term producer
(if (string=? separator "") ";" ","))))
(begin
(display "]")
(call-with-current-continuation producer)))))
 
;;;-------------------------------------------------------------------
 
(define (show fraction)
(display fraction)
(display " => ")
(r2cf fraction display-cf)
(newline))
 
(show 1/2)
(show 3)
(show 23/8)
(show 13/11)
(show 22/11)
(show -151/77)
(show 14142/10000)
(show 141421/100000)
(show 1414214/1000000)
(show 14142136/10000000)
(show 1414213562373095049/1000000000000000000)
 
;; Decimal expansion of sqrt(2): see https://oeis.org/A002193
(show 141421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157/100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000)
 
(show 31/10)
(show 314/100)
(show 3142/1000)
(show 31428/10000)
(show 314285/100000)
(show 3142857/1000000)
(show 31428571/10000000)
(show 314285714/100000000)
(show 3142857142857143/1000000000000000)
 
;; Decimal expansion of pi: see https://oeis.org/A000796
(show 314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214/100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000)
</syntaxhighlight>
 
{{out}}
<pre>$ gosh continued-fraction-from-rational-callcc.scm
1/2 => [0;2]
3 => [3]
23/8 => [2;1,7]
13/11 => [1;5,2]
2 => [2]
-151/77 => [-2;25,1,2]
7071/5000 => [1;2,2,2,2,2,1,1,29]
141421/100000 => [1;2,2,2,2,2,2,3,1,1,3,1,7,2]
707107/500000 => [1;2,2,2,2,2,2,2,3,6,1,2,1,12]
1767767/1250000 => [1;2,2,2,2,2,2,2,2,2,6,1,2,4,1,1,2]
1414213562373095049/1000000000000000000 => [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,1,39,1,5,1,3,61,1,1,8,1,2,1,7,1,1,6]
141421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157/100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 => [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,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,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,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,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,5,1,8,3,1,2,2,1,6,2,2,3,1,2,3,1,1,39,1,3,1,2,4,10,1,6,1,30,5,2,1,1,1,1,1,32,1,4,18,124,3,2,1,1,8,1,1,1,6,15,2,3,2,7,1,4,9,2,7,1,1,1,1,1,2,1,10,1,31,5,1,1,1,7,1,14,10,3,11,1,2,1,65,4,9,2,3,2,2,9,1,1,2,1,1,2]
31/10 => [3;10]
157/50 => [3;7,7]
1571/500 => [3;7,23,1,2]
7857/2500 => [3;7,357]
62857/20000 => [3;7,2857]
3142857/1000000 => [3;7,142857]
31428571/10000000 => [3;7,476190,3]
157142857/50000000 => [3;7,7142857]
3142857142857143/1000000000000000 => [3;6,1,142857142857142]
157079632679489661923132169163975144209858469968755291048747229615390820314310449931401741267105853399107/50000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 => [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,3,13,1,4,2,6,6,99,1,2,2,6,3,5,1,1,6,8,1,7,1,2,3,7,1,2,1,1,12,1,1,1,3,1,1,8,1,1,2,1,6,1,1,5,2,2,3,1,2,4,4,16,1,161,45,1,22,1,2,2,1,4,1,2,24,1,2,1,3,1,2,1,1,10,2,8,2,1,4,1,1,2,3,6,8,1,1,1,7,1,1,1,1,21,1,2,1,2,1,1,21,1,6,1,2,2,1,1,2,5,2,3,2,9,3,3,2,2,1,1,3,7,3,1,8,36,20,6,17,15,1,2,5,1,4,9,6,26,1,1,1,7,1,79,4,1,1,10,4,5,1,1,55,8,1,4,4,10,5,3,1,2,6,1,2,1,1,2,1,20,3,1,1,2,3]
</pre>
 
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">func r2cf(num, den) {
func() {
den.is_zero &&|| return() nil
var q = int(num//den)
(num, den) = (den, num - q*den)
return q
Line 975 ⟶ 4,850:
[314285714/100000000],
].each { |seq|
seq.each { |r| showcf(r2cf(r.partsnude)) }
print "\n"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 999 ⟶ 4,874:
{{trans|Ruby}}
===Direct translation===
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
 
proc r2cf {n1 {n2 1}} {
Line 1,017 ⟶ 4,892:
return -code break
}} $n1 $n2
}</langsyntaxhighlight>
Demonstrating:
<langsyntaxhighlight lang="tcl">proc printcf {name cf} {
puts -nonewline "$name -> "
while 1 {
Line 1,049 ⟶ 4,924:
} {
printcf "\[$n1;$n2\]" [r2cf $n1 $n2]
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,073 ⟶ 4,948:
</pre>
===Objectified version===
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
 
# General generator class based on coroutines
Line 1,181 ⟶ 5,056:
# Demonstrate parsing of input in forms other than a direct pair of decimals
printcf "1.5" [R2CF new 1.5]
printcf "23/7" [R2CF new 23/7]</langsyntaxhighlight>
{{out}}
<pre>
Line 1,205 ⟶ 5,080:
1.5 -> 1,2
23/7 -> 3,3,2
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-rat}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./rat" for Rat
import "./fmt" for Fmt
 
var toContFrac = Fn.new { |r|
var a = r.num
var b = r.den
while (true) {
Fiber.yield((a/b).truncate)
var t = a % b
a = b
b = t
if (a == 1) return
}
}
 
var groups = [
[ [1, 2], [3, 1], [23, 8], [13, 11], [22, 7], [-151, 77] ],
[ [14142, 1e4], [141421, 1e5], [1414214, 1e6], [14142136, 1e7] ],
[ [31, 10], [314, 100], [3142, 1e3], [31428, 1e4], [314285, 1e5], [3142857, 1e6],
[31428571, 1e7], [314285714,1e8]]
]
 
var lengths = [ [4, 2], [8, 8], [9, 9] ]
var headings = [ "Examples ->", "Sqrt(2) ->", "Pi ->" ]
var i = 0
for (group in groups) {
System.print(headings[i])
for (pair in group) {
Fmt.write("$*d / $*d = ", lengths[i][0], pair[0], -lengths[i][1], pair[1])
var f = Fiber.new(toContFrac)
var r = Rat.new(pair[0], pair[1])
while (!f.isDone) {
var d = f.call(r)
if (d) System.write("%(d) ")
}
System.print()
}
System.print()
i = i + 1
}</syntaxhighlight>
 
{{out}}
<pre>
Examples ->
1 / 2 = 0 2
3 / 1 = 3
23 / 8 = 2 1 7
13 / 11 = 1 5 2
22 / 7 = 3 7
-151 / 77 = -1 -1 -24 -1 -2
 
Sqrt(2) ->
14142 / 10000 = 1 2 2 2 2 2 1 1 29
141421 / 100000 = 1 2 2 2 2 2 2 3 1 1 3 1 7 2
1414214 / 1000000 = 1 2 2 2 2 2 2 2 3 6 1 2 1 12
14142136 / 10000000 = 1 2 2 2 2 2 2 2 2 2 6 1 2 4 1 1 2
 
Pi ->
31 / 10 = 3 10
314 / 100 = 3 7 7
3142 / 1000 = 3 7 23 1 2
31428 / 10000 = 3 7 357
314285 / 100000 = 3 7 2857
3142857 / 1000000 = 3 7 142857
31428571 / 10000000 = 3 7 476190 3
314285714 / 100000000 = 3 7 7142857
</pre>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes;
real Val;
 
Line 1,232 ⟶ 5,178:
RlOut(0, Val); CrLf(0);
I:= I+2];
]</langsyntaxhighlight>
{{out}}
<pre>
Line 1,246 ⟶ 5,192:
 
Light weight, explicit state:
<langsyntaxhighlight lang="zkl">fcn r2cf(nom,dnom){ // -->Walker (iterator)
Walker.tweak(fcn(_,state){
nom,dnom:=state;
if(dnom==0) return(Void.Stop);
Line 1,253 ⟶ 5,199:
state.clear(dnom,d);
n
}.fp1fp(List(nom,dnom))) // partial application (light weight closure)
}</langsyntaxhighlight>
Heavy weight, implicit state:
<langsyntaxhighlight lang="zkl">fcn r2cf2(nom,dnom){ // -->Generator (heavy weight Walker)
Utils.Generator(fcn(nom,dnom){
while(dnom){
Line 1,264 ⟶ 5,210:
Void.Stop;
},nom,dnom)
}</langsyntaxhighlight>
Both of the above return an iterator so they function the same:
<langsyntaxhighlight lang="zkl">foreach nom,dnom in (T(T(1,2), T(3,1), T(23,8), T(13,11), T(22,7),
T(14142,10000), T(141421,100000), T(1414214,1000000),
T(14142136,10000000))){
r2cf(nom,dnom).walk(25).println(); // print up to 25 numbers
}</langsyntaxhighlight>
{{out}}
<pre>
9,482

edits