Continued fraction convergents: Difference between revisions

m
→‎{{header|Phix}}: forgot the heading
m (spacing)
m (→‎{{header|Phix}}: forgot the heading)
 
(4 intermediate revisions by 3 users not shown)
Line 61:
=={{header|Phix}}==
{{trans|Wren}}
<!--(phixonline)-->
<syntaxhighlight lang="phix">
with javascript_semantics
Line 69 ⟶ 70:
sequence p = {mpq_init(0), mpq_init(1)},
q = {mpq_init(1), mpq_init(0)},
s = {{sprintf("= %d/%d =",{m,n}),m/n}}
t = sprintf("%d/%d",{m,n})
mpq r = mpq_init_set_si(m, n),
rem = mpq_init_set(r)
Line 82 ⟶ 84:
p &= pn
q &= qn
s &= {{mpq_get_str(sn),mpq_get_d(sn)}}
if mpq_cmp(r,sn)=0 then exit end if
mpq_sub(rem,rem,whole)
mpq_inv(rem,rem)
end while
printf(1,"%14s = %s\n",{t,join(s,"\n",fmt:="%-11s = %f")})
end procedure
 
procedure cfcQuad(string d, atom v, integer a, b, m, n, k)
sequence p = {0, 1},
q = {1, 0},
s = {{sprintf("= %s =",d),v}}
atom rem = (sqrt(a)*b + m) / n
for i=1 to k do
Line 102 ⟶ 104:
p &= pn
q &= qn
s &= {{mpq_get_str(sn),mpq_get_d(sn)}}
rem = 1/(rem-whole)
end for
printf(1,"%14s = %s\n",{d,join(s,"\n",fmt:="%-11s = %f")})
end procedure
 
printf(1,"The continued fraction convergents for the following (maximum 8 terms) are:\n")
cfcRat(415,93)
cfcRat(649,200)
cfcQuad("sqrt(2)",sqrt(2),2, 1, 0, 1, 8)
cfcQuad("sqrt(5)",sqrt(5),5, 1, 0, 1, 8)
cfcQuad("phigolden ratio",(sqrt(5)+1)/2,5, 1, 1, 2, 8)
 
</syntaxhighlight>
{{out}}
<pre>
The continued fraction convergents for the following (maximum 8 terms) are:
= 415/93 = = 4.462366
4 415/93 = 4.000000 9/2 58/13 415/93
9/2 649/200 = 3 13/4.500000 159/49 649/200
sqrt(2) = 1 3/2 7/5 17/12 41/29 99/70 239/169 577/408
58/13 = 4.461538
sqrt(5) = 2 9/4 38/17 161/72 682/305 2889/1292 12238/5473 51841/23184
415/93 = 4.462366
golden ratio = 1 2 3/2 5/3 8/5 13/8 21/13 34/21
= 649/200 = = 3.245000
</pre>
3 = 3.000000
 
13/4 = 3.250000
=={{header|Raku}}==
159/49 = 3.244898
{{trans|Julia}}
649/200 = 3.245000
<syntaxhighlight lang="raku" line># 20240210 Raku programming solution
= sqrt(2) = = 1.414214
 
1 = 1.000000
sub convergents(Real $x is copy, Int $maxcount) {
3/2 = 1.500000
my @components = gather for ^$maxcount {
7/5 = 1.400000
17/12 my $fpart = 1$x - take $x.416667Int;
$fpart == 0 ?? ( last ) !! ( $x = 1 / $fpart )
41/29 = 1.413793
}
99/70 = 1.414286
my ($numa, $denoma, $numb, $denomb) = 1, 0, @components[0], 1;
239/169 = 1.414201
return [ Rat.new($numb, $denomb) ].append: gather for @components[1..*] -> $comp {
577/408 = 1.414216
( $numa, $denoma, $numb , $denomb )
= sqrt(5) = = 2.236068
= $numb, $denomb, $numa + $comp * $numb, $denoma + $comp * $denomb;
2 = 2.000000
take Rat.new($numb, $denomb);
9/4 = 2.250000
}
38/17 = 2.235294
}
161/72 = 2.236111
 
682/305 = 2.236066
my @tests = [ "415/93", 415/93, "649/200", 649/200, "sqrt(2)", sqrt(2),
2889/1292 = 2.236068
"sqrt(5)", sqrt(5), "golden ratio", (sqrt(5) + 1) / 2 ];
12238/5473 = 2.236068
 
51841/23184 = 2.236068
say "The continued fraction convergents for the following (maximum 8 terms) are:";
= phi = = 1.618034
for @tests -> $s, $x {
1 = 1.000000
say $s.fmt('%15s') ~ " = { convergents($x, 8).map: *.nude.join('/') } ";
2 = 2.000000
}</syntaxhighlight>
3/2 = 1.500000
{{out}}
5/3 = 1.666667
<pre>
8/5 = 1.600000
The continued fraction convergents for the following (maximum 8 terms) are:
13/8 = 1.625000
21/13 415/93 = 4/1.615385 9/2 58/13 415/93
34/21 649/200 = 3/1.619048 13/4 159/49 649/200
sqrt(2) = 1/1 3/2 7/5 17/12 41/29 99/70 239/169 577/408
sqrt(5) = 2/1 9/4 38/17 161/72 682/305 2889/1292 12238/5473 51841/23184
golden ratio = 1/1 2/1 3/2 5/3 8/5 13/8 21/13 34/21
</pre>
You may [https://ato.pxeger.com/run?1=dVNNb9pAED305l_xYrliTRdj01AREE2vvUa5ISotsCZu7LW7u25AEfkjveTQ_qn8mo6_EKDWF8_OvDezM2_n128tHsvX1z-ljQeTt3fClCusc_VT6q1U1rA7KVJ4OySG3MWe46uy8DKxW-elsj6eHQDZHl_WeVbkquJgjq2wD1IjzjW-HcENtoF7cSG0JSSlHsCKR0lWQLlnLaYDzBHi9hYMqTAWPq6uyCbSHBGGHcqvSIf2JsxTZSY4vI1UeW3QedWdV35F5Qj56ZUX4ZIjqmtraUutsMCdsIGST-ySvgxEUUi1mZ52eZorCoL-EoPP8CrnsWuGf14MF9-x0mUA1H2ban7RU5NK4ENbsn8ePws0jG7M9eT_0-msmerBcSp9rTS1tAu419F4ePPR5WgMDvfT9c1wFIbkai3ymR_aspFPvtbiznk7DWJ8RIx9Ym3zlMpDC5vkFGBthDqIfBJ8VDOXM8cxYg_3_kFWr9UmqpQbxFqsiaZOH3CtDslE_zTNnxK1BaMHmWRlhgms1JnxIbScujOnFrLps1LP8Oqh1fJVxTwTxJllvffR2PR8vMClaTyfLYu345j4QSaKKfqBKjcy-J4nivWGRDiAShycZtPahesW7y8 Attempt This Online!]
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func num2cfrac(n, r) {
gather {
r.times {
n = 1/((n - take(n.floor.int)) || break)
}
}
}
 
func convergents(x, n) {
var cfrac = num2cfrac(x, n)
 
var(n1, n2) = (0, 1)
var(d1, d2) = (1, 0)
 
gather {
for z in (cfrac) {
(n1, n2) = (n2, n2*z + n1)
(d1, d2) = (d2, d2*z + d1)
take(n2/d2)
}
}
}
 
var tests = ["415/93", 415/93, "649/200", 649/200, "sqrt(2)", sqrt(2),
"sqrt(5)", sqrt(5), "golden ratio", (sqrt(5) + 1) / 2 ]
 
var terms = 8
say "The continued fraction convergents for the following (maximum #{terms} terms) are:"
tests.each_slice(2, {|s,x|
printf("%15s = %s\n", s, convergents(x, terms).map { .as_frac }.join(' '))
})</syntaxhighlight>
{{out}}
<pre>
The continued fraction convergents for the following (maximum 8 terms) are:
415/93 = 4/1 9/2 58/13 415/93
649/200 = 3/1 13/4 159/49 649/200
sqrt(2) = 1/1 3/2 7/5 17/12 41/29 99/70 239/169 577/408
sqrt(5) = 2/1 9/4 38/17 161/72 682/305 2889/1292 12238/5473 51841/23184
golden ratio = 1/1 2/1 3/2 5/3 8/5 13/8 21/13 34/21
</pre>
 
7,795

edits