Continued fraction/Arithmetic/Construct from rational number: Difference between revisions
Content added Content deleted
m (Added Sidef) |
|||
Line 949: | Line 949: | ||
31428571 / 10000000 : 3 7 476190 3 |
31428571 / 10000000 : 3 7 476190 3 |
||
314285714 / 100000000 : 3 7 7142857 |
314285714 / 100000000 : 3 7 7142857 |
||
</pre> |
|||
=={{header|Sidef}}== |
|||
{{trans|Perl}} |
|||
<lang ruby>func r2cf(num, den) { |
|||
func() { |
|||
den.is_zero && return() |
|||
var q = int(num/den) |
|||
(num, den) = (den, num - q*den) |
|||
return q |
|||
} |
|||
} |
|||
func showcf(f) { |
|||
print "[" |
|||
var n = f() |
|||
print "#{n}" if defined(n) |
|||
print "; #{n}" while defined(n = f()) |
|||
print "]\n" |
|||
} |
|||
[ |
|||
[1/2, 3/1, 23/8, 13/11, 22/7, -151/77], |
|||
[14142/10000, 141421/100000, 1414214/1000000, 14142136/10000000], |
|||
[314285714/100000000], |
|||
].each { |seq| |
|||
seq.each { |r| showcf(r2cf(r.parts)) } |
|||
print "\n" |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
[0; 2] |
|||
[3] |
|||
[2; 1; 7] |
|||
[1; 5; 2] |
|||
[3; 7] |
|||
[-1; -1; -24; -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; 7; 7142857] |
|||
</pre> |
</pre> |
||