Pythagorean triples: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
imported>Maxima enthusiast
No edit summary
m (→‎{{header|Wren}}: Changed to Wren S/H)
(5 intermediate revisions by 3 users not shown)
Line 381:
Up to 10 ** 8 : 113236940 Triples, 7023027 Primitives
Up to 10 ** 9 : 1294080089 Triples, 70230484 Primitives</pre>
 
=={{header|ALGOL 68}}==
Uses a table of square roots so OK for perimeters up to 1000 (or possibly 10 000).
<syntaxhighlight lang="algol68">
BEGIN # find some Pythagorean triples ( a, b, c ) #
# where a < b < c and a^2 + b^2 = c^2 #
 
INT max perimeter = 100; # maximum a + b + c we will consider #
INT max square = max perimeter * max perimeter;
# form a table of square roots of numbers to max perimeter ^ 2 #
[ 1 : max square ]INT sr;
FOR i TO UPB sr DO sr[ i ] := 0 OD;
FOR i TO max perimeter DO sr[ i * i ] := i OD;
 
PROC gcd = ( INT x, y )INT: # iterative gcd #
BEGIN
INT a := ABS x, b := ABS y;
WHILE b /= 0 DO
INT next a = b;
b := a MOD b;
a := next a
OD;
a
END # gcd # ;
 
# count the Pythagorean triples #
INT t count := 0, p count := 0;
FOR a TO max perimeter DO
INT a2 = a * a;
FOR b FROM a + 1 TO max perimeter - a
WHILE INT c = sr[ a2 + ( b * b ) ];
a + b + c <= max perimeter
DO IF c > b THEN # have a triple #
t count +:= 1;
IF gcd( a, b ) = 1 THEN # have a primitive triple #
p count +:= 1
FI
FI
OD
OD;
print( ( "Pythagorean triples with perimeters up to ", whole( max perimeter, 0 ), ":", newline ) );
print( ( " Primitive: ", whole( p count, 0 ), newline ) );
print( ( " Total: ", whole( t count, 0 ), newline ) )
 
END
</syntaxhighlight>
{{out}}
<pre>
Pythagorean triples with perimeters up to 100:
Primitive: 7
Total: 17
</pre>
 
=={{header|Arturo}}==
Line 1,345 ⟶ 1,397:
=={{header|Delphi}}==
See [[#Pascal|Pascal]].
 
=={{header|EasyLang}}==
{{trans|C}}
<syntaxhighlight>
global total prim maxperi .
proc newtri s0 s1 s2 . .
p = s0 + s1 + s2
if p <= maxperi
prim += 1
total += maxperi div p
newtri s0 - 2 * s1 + 2 * s2 2 * s0 - s1 + 2 * s2 2 * s0 - 2 * s1 + 3 * s2
newtri s0 + 2 * s1 + 2 * s2 2 * s0 + s1 + 2 * s2 2 * s0 + 2 * s1 + 3 * s2
newtri -s0 + 2 * s1 + 2 * s2 -2 * s0 + s1 + 2 * s2 -2 * s0 + 2 * s1 + 3 * s2
.
.
for maxperi in [ 100 10000000 ]
prim = 0
total = 0
newtri 3 4 5
print "Up to " & maxperi & ": " & total & " triples, " & prim & " primitives"
.
</syntaxhighlight>
 
=={{header|EDSAC order code}}==
Line 3,345 ⟶ 3,419:
/* Test case */
/* With the function param_pythag as it is some non primitive triples are missing, but not the primitives */
sublist(param_pythag(6),lambda([x],primitivep(x) and perim(x)<=100)),;
block(
 
sublist(param_pythag(6),lambda([x],perim(x)<=100)),
sublist(%%,primitivep));
 
/* The number of triples, primitive or not, can be recovered from the primitives */
Line 5,249 ⟶ 5,322:
{{trans|Go}}
Limited to a maximum perimeter of 10 billion in order to finish in a reasonable time.
<syntaxhighlight lang="ecmascriptwren">var sc = System.clock
var total = 0
var prim = 0
9,483

edits