Pythagorean triples: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(→‎{{header|Raku}}: First, brute-force solution was not restricting results to the perimeter limit.)
m (→‎{{header|Wren}}: Changed to Wren S/H)
(8 intermediate revisions by 4 users not shown)
Line 382:
Up to 10 ** 9 : 1294080089 Triples, 70230484 Primitives</pre>
 
=={{header|ANSIALGOL Standard BASIC68}}==
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 #
Translation of BBC BASIC Program
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 #
<syntaxhighlight lang="ansi standard basic">100 DECLARE EXTERNAL SUB tri
BEGIN
110 !
INT a := ABS x, b := ABS y;
120 PUBLIC NUMERIC U0(3,3), U1(3,3), U2(3,3), all, prim
WHILE b /= 0 DO
130 DIM seed(3)
INT next a = b;
140 MAT READ U0, U1, U2
b := a MOD b;
150 DATA 1, -2, 2, 2, -1, 2, 2, -2, 3
160 DATA 1, 2, 2, 2, 1, 2, 2, 2, 3 a := next a
170 DATA -1, 2, 2, -2, 1, 2, -2, 2, 3 OD;
a
180 !
END # gcd # ;
190 MAT READ seed
 
200 DATA 3, 4, 5
# count the Pythagorean triples #
210 FOR power = 1 TO 7
220 INT LETt allcount := 0, p count := 0;
230 FOR LETa primTO max =perimeter 0DO
240 CALL tri(seed, 10^power ,INT alla2 ,= prim)a * a;
FOR b FROM a + 1 TO max perimeter - a
250 PRINT "Up to 10^";power,
WHILE INT c = sr[ a2 + ( b * b ) ];
260 PRINT USING "######### triples ######### primitives":all,prim
a + b + c <= max perimeter
270 NEXT power
DO IF c > b THEN # have a triple #
280 END
t count +:= 1;
290 !
IF gcd( a, b ) = 1 THEN # have a primitive triple #
300 EXTERNAL SUB tri(i(), mp, all, prim)
p count +:= 1
310 DECLARE EXTERNAL FUNCTION SUM
FI
320 DECLARE NUMERIC t(3)
FI
330 !
340 IF SUM(i) > mp THEN EXIT SUBOD
OD;
350 LET prim = prim + 1
print( ( "Pythagorean triples with perimeters up to ", whole( max perimeter, 0 ), ":", newline ) );
360 LET all = all + INT(mp / SUM(i))
print( ( " Primitive: ", whole( p count, 0 ), newline ) );
370 !
print( ( " Total: ", whole( t count, 0 ), newline ) )
380 MAT t = U0 * i
 
390 CALL tri(t, mp , all , prim)
END
400 MAT t = U1 * i
</syntaxhighlight>
410 CALL tri(t, mp , all , prim)
{{out}}
420 MAT t = U2 * i
<pre>
430 CALL tri(t, mp , all , prim)
Pythagorean triples with perimeters up to 100:
440 END SUB
Primitive: 7
450 !
Total: 17
460 EXTERNAL FUNCTION SUM(a())
</pre>
470 LET temp = 0
480 FOR i=LBOUND(a) TO UBOUND(a)
490 LET temp = temp + a(i)
500 NEXT i
510 LET SUM = temp
520 END FUNCTION</syntaxhighlight>
 
=={{header|Arturo}}==
Line 588 ⟶ 592:
</pre>
 
=={{header|BBC BASIC}}==
==={{header|ANSI BASIC}}===
{{trans|BBC BASIC}}
{{works with|Decimal BASIC}}
<syntaxhighlight lang="basic">100 DECLARE EXTERNAL SUB tri
110 !
120 PUBLIC NUMERIC U0(3,3), U1(3,3), U2(3,3), all, prim
130 DIM seed(3)
140 MAT READ U0, U1, U2
150 DATA 1, -2, 2, 2, -1, 2, 2, -2, 3
160 DATA 1, 2, 2, 2, 1, 2, 2, 2, 3
170 DATA -1, 2, 2, -2, 1, 2, -2, 2, 3
180 !
190 MAT READ seed
200 DATA 3, 4, 5
210 FOR power = 1 TO 7
220 LET all = 0
230 LET prim = 0
240 CALL tri(seed, 10^power , all , prim)
250 PRINT "Up to 10^";power,
260 PRINT USING "######### triples ######### primitives":all,prim
270 NEXT power
280 END
290 !
300 EXTERNAL SUB tri(i(), mp, all, prim)
310 DECLARE EXTERNAL FUNCTION SUM
320 DECLARE NUMERIC t(3)
330 !
340 IF SUM(i) > mp THEN EXIT SUB
350 LET prim = prim + 1
360 LET all = all + INT(mp / SUM(i))
370 !
380 MAT t = U0 * i
390 CALL tri(t, mp , all , prim)
400 MAT t = U1 * i
410 CALL tri(t, mp , all , prim)
420 MAT t = U2 * i
430 CALL tri(t, mp , all , prim)
440 END SUB
450 !
460 EXTERNAL FUNCTION SUM(a())
470 LET temp = 0
480 FOR i=LBOUND(a) TO UBOUND(a)
490 LET temp = temp + a(i)
500 NEXT i
510 LET SUM = temp
520 END FUNCTION</syntaxhighlight>
{{out}}
<pre>
Up to 10^ 1 0 triples 0 primitives
Up to 10^ 2 17 triples 7 primitives
Up to 10^ 3 325 triples 70 primitives
Up to 10^ 4 4858 triples 703 primitives
Up to 10^ 5 64741 triples 7026 primitives
Up to 10^ 6 808950 triples 70229 primitives
Up to 10^ 7 9706567 triples 702309 primitives
</pre>
 
==={{header|BBC BASIC}}===
The built-in array arithmetic is very well suited to this task!
<syntaxhighlight lang="bbcbasic"> DIM U0%(2,2), U1%(2,2), U2%(2,2), seed%(2)
Line 1,335 ⟶ 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,315 ⟶ 3,399:
Output:
<pre> There are 17 Pythagorean Triples and 7 primitive triples with a perimeter smaller than 100.</pre>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
/* Function that returns a pythagorean triple from two parameters */
pythag(u,v):=[u^2-v^2,2*u*v,u^2+v^2]$
 
/* Predicate function to check for primitivity */
primitivep(lst):=if lreduce('gcd,lst)=1 then true$
 
/* Function that returns perimeter */
perim(lst):=apply("+",lst)$
 
/* Function to return a list of triples by parameter u */
/* Parameter v is controlled to be lesser or equal than u, and when equal are deleted */
param_pythag(n):=block(
create_list(lambda([x,y],pythag(x,y) and x#y)(i,j),i,1,n,j,1,i),
delete(false,%%))$
 
/* 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));
 
 
/* The number of triples, primitive or not, can be recovered from the primitives */
block(
apply(append,makelist(%*i,i,1,8)),
sublist(%%,lambda([x],perim(x)<=100)));
</syntaxhighlight>
{{out}}
<pre>
[[3,4,5],[5,12,13],[15,8,17],[7,24,25],[21,20,29],[9,40,41],[35,12,37]]
 
[[3,4,5],[5,12,13],[15,8,17],[7,24,25],[21,20,29],[9,40,41],[35,12,37],[6,8,10],[10,24,26],[30,16,34],[9,12,15],[15,36,39],[12,16,20],[15,20,25],[18,24,30],[21,28,35],[24,32,40]]
</pre>
 
=={{header|Mercury}}==
Line 5,204 ⟶ 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,482

edits