Continued fraction/Arithmetic/Construct from rational number: Difference between revisions
Content added Content deleted
No edit summary |
(perl 6 entry) |
||
Line 22: | Line 22: | ||
Demonstrate that this function may be used as generator a in [[http://rosettacode.org/wiki/Continued_fraction Continued fraction]] and obtain a floating point value for 23/8, 13/11, and 22/7 |
Demonstrate that this function may be used as generator a in [[http://rosettacode.org/wiki/Continued_fraction Continued fraction]] and obtain a floating point value for 23/8, 13/11, and 22/7 |
||
=={{header|Perl 6}}== |
|||
Straightforward implementation: |
|||
<lang perl 6>sub r2cf(Rat $x is copy) { |
|||
gather while True { |
|||
$x -= take $x.Int; |
|||
last unless $x > 0; |
|||
$x = 1 / $x; |
|||
} |
|||
} |
|||
say r2cf(Rat($_)) for <1/2 3 23/8 13/11 22/7 1.41 1.4142136>;</lang> |
|||
{{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> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |