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

m
 
(12 intermediate revisions by 4 users not shown)
Line 492:
 
=== 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.
 
Line 505 ⟶ 509:
`pkg-config --cflags ats2-xprelude` -O2 -std=gnu2x \
continued-fraction-from-rational-2.dats \
`pkg-config --libs ats2-xprelude` -lgc -lm && ./a.out
 
*)
Line 1,890 ⟶ 1,894:
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}}==
Line 3,374 ⟶ 3,501:
 
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">
Line 3,420 ⟶ 3,549:
(if (Denominator = zero)
then (CF = lazy_list(delay((func) = nil)))
else (CF = lazy_list(delay((func) = cons(Quotient, Tail)Cons)),
Tail((func) = r2cf(Denominator,Cell Remainder),:-
(Cell = cons(Quotient, r2cf(Denominator, Remainder)),
%% What follows is division with truncation towards zero.
%% What follows is division with truncation towards zero.
divide_with_rem(Numerator, Denominator,
divide_with_rem(Numerator, QuotientDenominator, Remainder))).
Quotient, Remainder))) = Cons)).
 
%%%-------------------------------------------------------------------
Line 3,526 ⟶ 3,656:
 
{{out}}
<pre>$ mmc --use-subdirs continued_fraction_from_rational_lazy.m && ./continued_fraction_from_rational_lazy
1/2 => [0;2]
3 => [3]
Line 4,136 ⟶ 4,266:
───────── 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>
 
Line 4,915 ⟶ 5,085:
{{libheader|Wren-rat}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./rat" for Rat
import "./fmt" for Fmt
 
var toContFrac = Fn.new { |r|
9,476

edits