Special pythagorean triplet: Difference between revisions
Content added Content deleted
(→{{header|ALGOL 68}}: Replaced with a solution using Euclid's formula, based on the XPL0 sample) |
(→{{header|ALGOL 68}}: Stop after a solution is found.) |
||
Line 7: | Line 7: | ||
<br><br> |
<br><br> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
Using Euclid's formula, as in the XPL0 sample |
Using Euclid's formula, as in the XPL0 sample. |
||
...but doesn't stop on the first solution (thus verifying there is only one). |
|||
<lang algol68>BEGIN # find the product of the of the Pythagorian triplet a, b, c where: # |
<lang algol68>BEGIN # find the product of the of the Pythagorian triplet a, b, c where: # |
||
# a + b + c = 1000, a2 + b2 = c2, a < b < c # |
# a + b + c = 1000, a2 + b2 = c2, a < b < c # |
||
INT sqrt 1000 = ENTIER sqrt( 1000 ); |
INT sqrt 1000 = ENTIER sqrt( 1000 ); |
||
BOOL found := FALSE; |
|||
FOR n TO sqrt 1000 OVER 2 WHILE NOT found DO |
|||
FOR m FROM n + 1 TO sqrt 1000 WHILE NOT found DO |
|||
# a = m^2 - n^2, b = 2mn, c = m^2 + n^2 ( Euclid's formula ), so # |
# a = m^2 - n^2, b = 2mn, c = m^2 + n^2 ( Euclid's formula ), so # |
||
# a + b + c = m^2 - n^2 + 2mn + m^2 + n^2 = 2( m^2 + mn ) = 2m( m + n ) # |
# a + b + c = m^2 - n^2 + 2mn + m^2 + n^2 = 2( m^2 + mn ) = 2m( m + n ) # |
||
Line 22: | Line 22: | ||
INT c = m2 + n2; |
INT c = m2 + n2; |
||
print( ( "a = ", whole( a, 0 ), ", b = ", whole( b, 0 ), ", c = ", whole( c, 0 ), newline ) ); |
print( ( "a = ", whole( a, 0 ), ", b = ", whole( b, 0 ), ", c = ", whole( c, 0 ), newline ) ); |
||
print( ( "a * b * c = ", whole( a * b * c, 0 ), newline ) ) |
print( ( "a * b * c = ", whole( a * b * c, 0 ), newline ) ); |
||
found := TRUE |
|||
FI |
FI |
||
OD |
OD |
||
OD |
OD |
||
END |
END</lang> |
||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |