Pythagorean triples: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: changed and add comments, changed some REXX variable names.)
m (→‎using single evenness for determinacy: added and changed some comments, changed some REXX variable names.)
Line 3,218: Line 3,218:
<lang rexx>/*REXX program counts the number of Pythagorean triples that exist given a maximum */
<lang rexx>/*REXX program counts the number of Pythagorean triples that exist given a maximum */
/*──────────────────── perimeter of N, and also counts how many of them are primitives.*/
/*──────────────────── perimeter of N, and also counts how many of them are primitives.*/
@.=0; trips=0; prims=0 /*define some REXX variables to zero. */
parse arg N . /*obtain optional argument from the CL.*/
parse arg N . /*obtain optional argument from the CL.*/
if N=='' | N=="," then n=100 /*Not specified? Then use the default.*/
if N=='' | N=="," then n=100 /*Not specified? Then use the default.*/
T=0; P=0 /*set the number of Triples, Primitives*/

do a=3 to N%3; aa=a*a /*limit side to 1/3 of the perimeter.*/
@.=0; do a=3 to N%3; aa=a*a /*limit side to 1/3 of the perimeter.*/
aEven= a//2==0 /*set variable to 1 if A is even. */
aEven= a//2==0 /*set variable to 1 if A is even. */
do b=a+1 by 1+aEven /*the triangle can't be isosceles. */

do b=a+1 by 1+aEven /*the triangle can't be isosceles. */
ab=a + b /*compute a partial perimeter (2 sides)*/
ab=a + b /*compute a partial perimeter (2 sides)*/
if ab>=N then iterate a /*is a+b perimeter? Try different A*/
if ab>=N then iterate a /*is a+b perimeter? Try different A*/
aabb=aa + b*b /*compute the sum of a²+b² (shortcut)*/
aabb=aa + b*b /*compute the sum of a²+b² (shortcut)*/
do c=b + 1 /*compute the value of the third side. */
if aEven then if c//2==0 then iterate

do c=b + 1 /*compute the value of the third side. */
if ab+c>n then iterate a /*a+b+c > perimeter? Try different A.*/
if aEven then if c//2==0 then iterate
cc=c*c /*compute the value of C². */
if ab+c>n then iterate a /*a+b+c > perimeter? Try different A.*/
if cc > aabb then iterate b /*is c² > a²+b² ? Try a different B.*/
cc=c*c /*compute the value of C². */
if cc\==aabb then iterate /*is ¬= a²+b² ? Try a different C.*/
if cc > aabb then iterate b /*is c² > a²+b² ? Try a different B.*/
if @.a.b.c then iterate /*Is this a duplicate? Then try again.*/
if cc\==aabb then iterate /*is ¬= a²+b² ? Try a different C.*/
T=T + 1 /*Eureka! We found a Pythagorean triple*/
if @.a.b.c then iterate /*Is this a duplicate? Then try again.*/
P=P + 1 /*count this also as a primitive triple*/
trips=trips + 1 /*Eureka! We found a Pythagorean triple*/
do m=2 while a*m+b*m+c*m<=N /*generate non-primitives Pythagoreans.*/
prims=prims + 1 /*count this also as a primitive triple*/
T=T + 1 /*Eureka! We found a Pythagorean triple*/
am=a*m; bm=b*m; cm=c*m /*create some short-cut variable names.*/

do m=2; am=a*m; bm=b*m; cm=c*m /*generate non-primitives Pythagoreans.*/
@.am.bm.cm=1 /*mark Pythagorean triangle as a triple*/
if am+bm+cm>N then leave /*is this multiple Pythagorean triple? */
end /*m*/
trips=trips+1 /*Eureka! We found a Pythagorean triple*/
end /*c*/
@.am.bm.cm=1 /*mark Pythagorean triangle as a triple*/
end /*b*/
end /*m*/
end /*a*/
end /*c*/
end /*b*/
end /*a*/

_=left('', 7) /*for padding the output with 7 blanks.*/
_=left('', 7) /*for padding the output with 7 blanks.*/
say 'max perimeter =' N _ "Pythagorean triples =" trips _ 'primitives =' prims
say 'max perimeter =' N _ "Pythagorean triples =" T _ 'primitives =' P
/*stick a fork in it, we're all done. */</lang>
/*stick a fork in it, we're all done. */</lang>
'''output''' &nbsp; is identical to the 1<sup>st</sup> REXX version.
'''output''' &nbsp; is identical to the 1<sup>st</sup> REXX version.