Continued fraction/Arithmetic/Construct from rational number: Difference between revisions
Continued fraction/Arithmetic/Construct from rational number (view source)
Revision as of 13:57, 3 April 2020
, 4 years ago→{{header|Phix}}: rewrote, floor()->trunc(), and improved output
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
m (→{{header|Phix}}: rewrote, floor()->trunc(), and improved output) |
||
Line 1,633:
=={{header|Phix}}==
atom quot = 0
▲<lang Phix>function r2cf(sequence fraction)
{num,denom} = {denom,num-quot*denom}
▲ if denominator!=0 then
end if
return {
end function
procedure test(string txt, sequence tests)
printf(1,"Running %s :\n",{txt})▼
▲ printf(1,"Running %s :",{txt})
for i=1 to length(tests) do
printf(1,"%d/%d ==> [",{num,denom})
while
printf(1,"%d",quot)
if denom=0 then exit end if
printf(1,"%s",sep)
sep = ","
end while
printf(1,"]\n")
end for
printf(1,"
end procedure
constant examples = {{1,2}, {3,1}, {23,8}, {13,11}, {22,7}, {-151,77}},
sqrt2 = {{14142,10000}, {141421,100000}, {1414214,1000000}, {14142136,10000000}},
pi = {{31,10}, {314,100}, {3142,1000}, {31428,10000}, {314285,100000}, {3142857,1000000},
{31428571,10000000}, {314285714,100000000}, {3141592653589793,1000000000000000}}
test("the examples",examples)
Line 1,670 ⟶ 1,671:
<pre>
Running the examples :
Running for sqrt(2) :
Running for pi :
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>
|