Jump to content

Continued fraction/Arithmetic/Construct from rational number: Difference between revisions

Added Algol 68
(Added Quackery.)
(Added Algol 68)
Line 66:
[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]
</pre>
 
=={{header|ALGOL 68}}==
{{Trans|C}}
...with code from the Arithmetic/Rational task.
<lang algol68>BEGIN # construct continued fraction representations of rational numbers #
# Translated from the C sample #
# Uses code from the Arithmetic/Rational task #
 
# Code from the Arithmetic/Rational task #
# ============================================================== #
 
MODE FRAC = STRUCT( INT num #erator#, den #ominator#);
 
PROC gcd = (INT a, b) INT: # greatest common divisor #
(a = 0 | b |: b = 0 | a |: ABS a > ABS b | gcd(b, a MOD b) | gcd(a, b MOD a));
PROC lcm = (INT a, b)INT: # least common multiple #
a OVER gcd(a, b) * b;
PRIO // = 9; # higher then the ** operator #
OP // = (INT num, den)FRAC: ( # initialise and normalise #
INT common = gcd(num, den);
IF den < 0 THEN
( -num OVER common, -den OVER common)
ELSE
( num OVER common, den OVER common)
FI
);
OP + = (FRAC a, b)FRAC: (
INT common = lcm(den OF a, den OF b);
FRAC result := ( common OVER den OF a * num OF a + common OVER den OF b * num OF b, common );
num OF result//den OF result
);
OP - = (FRAC a, b)FRAC: a + -b,
* = (FRAC a, b)FRAC: (
INT num = num OF a * num OF b,
den = den OF a * den OF b;
INT common = gcd(num, den);
(num OVER common) // (den OVER common)
);
OP - = (FRAC frac)FRAC: (-num OF frac, den OF frac);
# ============================================================== #
# end code from the Arithmetic/Rational task #
 
[]FRAC examples = ( 1//2, 3//1, 23//8, 13//11, 22//7, -151//77 );
[]FRAC sqrt2 = ( 14142//10000, 141421//100000, 1414214//1000000, 14142136//10000000 );
[]FRAC pi = ( 31//10, 314//100, 3142//1000, 31428//10000
, 314285//100000, 3142857//1000000, 31428571//10000000, 314285714//100000000
);
# returns the uotient of numerator over denominator and sets #
# numerator and denominator to the next values for #
# the continued fraction #
PROC r2cf = ( REF INT numerator, REF INT denominator )INT:
IF denominator = 0
THEN 0
ELSE INT quotient := numerator OVER denominator;
INT prev numerator = numerator;
numerator := denominator;
denominator := prev numerator MOD denominator;
quotient
FI # r2cf # ;
# shows the continued fractions for the elements of f seq #
PROC show r2cf = ( STRING legend, []FRAC f seq )VOID:
BEGIN
print( ( legend ) );
FOR i FROM LWB f seq TO UPB f seq DO
INT num := num OF f seq[ i ];
INT den := den OF f seq[ i ];
print( ( newline, "For N = ", whole( num , 0 ), ", D = ", whole( den , 0 ), " :" ) );
WHILE den /= 0 DO
print( ( " ", whole( r2cf( num, den ), 0 ) ) )
OD
OD
END # show r2cf # ;
BEGIN # task #
show r2cf( "Running the examples :", examples );
print( ( newline, newline ) );
show r2cf( "Running for root2 :", sqrt2 );
print( ( newline, newline ) );
show r2cf( "Running for pi :", pi )
END
END</lang>
{{out}}
<pre>
Running the examples :
For N = 1, D = 2 : 0 2
For N = 3, D = 1 : 3
For N = 23, D = 8 : 2 1 7
For N = 13, D = 11 : 1 5 2
For N = 22, D = 7 : 3 7
For N = -151, D = 77 : -1 25 1 2
 
Running for root2 :
For N = 7071, D = 5000 : 1 2 2 2 2 2 1 1 29
For N = 141421, D = 100000 : 1 2 2 2 2 2 2 3 1 1 3 1 7 2
For N = 707107, D = 500000 : 1 2 2 2 2 2 2 2 3 6 1 2 1 12
For N = 1767767, D = 1250000 : 1 2 2 2 2 2 2 2 2 2 6 1 2 4 1 1 2
 
Running for pi :
For N = 31, D = 10 : 3 10
For N = 157, D = 50 : 3 7 7
For N = 1571, D = 500 : 3 7 23 1 2
For N = 7857, D = 2500 : 3 7 357
For N = 62857, D = 20000 : 3 7 2857
For N = 3142857, D = 1000000 : 3 7 142857
For N = 31428571, D = 10000000 : 3 7 476190 3
For N = 157142857, D = 50000000 : 3 7 7142857
</pre>
 
3,048

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.