Continued fraction/Arithmetic/Construct from rational number: Difference between revisions
Content added Content deleted
(Added Algol 68) |
(→{{header|ALGOL 68}}: Fix negative numbers) |
||
Line 70: | Line 70: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
{{Trans|C}} |
{{Trans|C}} |
||
...with code from the Arithmetic/Rational task. |
...with code from the Arithmetic/Rational task.<br> |
||
Note that Algol 68's MOD operator uses the mathematical definition (rounds towards -infinity) which differs from C and many other languages. The remainder is calculated here as it would be in C, so the results for -157//77 agree with the other samples. |
|||
<lang algol68>BEGIN # construct continued fraction representations of rational numbers # |
<lang algol68>BEGIN # construct continued fraction representations of rational numbers # |
||
# Translated from the C sample # |
# Translated from the C sample # |
||
Line 130: | Line 131: | ||
INT prev numerator = numerator; |
INT prev numerator = numerator; |
||
numerator := denominator; |
numerator := denominator; |
||
denominator := prev numerator |
denominator := prev numerator - ( quotient * denominator ); |
||
quotient |
quotient |
||
FI # r2cf # ; |
FI # r2cf # ; |
||
Line 162: | Line 163: | ||
For N = 13, D = 11 : 1 5 2 |
For N = 13, D = 11 : 1 5 2 |
||
For N = 22, D = 7 : 3 7 |
For N = 22, D = 7 : 3 7 |
||
For N = -151, D = 77 : -1 |
For N = -151, D = 77 : -1 -1 -24 -1 -2 |
||
Running for root2 : |
Running for root2 : |