Pythagorean triples: Difference between revisions

Content deleted Content added
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:
<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.*/
@.=0; trips=0; prims=0 /*define some REXX variables to zero. */
parse arg N . /*obtain optional argument from the CL.*/
if N=='' | N=="," then n=100 /*Not specified? Then use the default.*/
T=0; P=0 /*set the number of Triples, Primitives*/
 
@.=0; do a=3 to N%3; aa=a*a /*limit side to 1/3 of the perimeter.*/
aEven= a//2==0 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 ab=a +1 b by 1+aEven /*the triangle can't be isosceles.compute a partial perimeter (2 sides)*/
ab=a + b if ab>=N then iterate a /*computeis a+b partial perimeter? Try (2different sides)A*/
if ab>=N then iterate a aabb=aa + b*b /*iscompute the a+bsum of perimeter? a²+b² Try different A(shortcut)*/
aabb=aa + b*b do c=b + 1 /*compute the sumvalue of the a²+b²third side. (shortcut)*/
if aEven then if c//2==0 then iterate
 
do c=b + 1 if ab+c>n then iterate a /*computea+b+c the> valueperimeter? of theTry thirddifferent side. A.*/
if aEven then if cc=c*c //2==0*compute the thenvalue iterateof C². */
if ab+c>n then iterate a if cc > aabb then iterate b /*a+b+is c² > perimeter a²+b² ? Try differenta different AB.*/
cc=c*c if cc\==aabb then iterate /*computeis the value of¬= Ca²+b². ? Try a different C.*/
if cc > aabb then iterate b if @.a.b.c then iterate /*is c² >Is this a²+b² duplicate? Try aThen differenttry Bagain.*/
if cc\ T==aabbT + then1 iterate /*is ¬= a²+b² ? Try /*Eureka! We found a differentPythagorean C.triple*/
if @.a.b.c then iterate P=P + 1 /*Iscount this aalso duplicate?as a Then tryprimitive again.triple*/
trips=trips + 1 do m=2 while a*m+b*m+c*m<=N /*Eureka! We found agenerate Pythagoreannon-primitives triplePythagoreans.*/
prims=prims + 1 T=T + 1 /*count thisEureka! alsoWe asfound a primitivePythagorean triple*/
am=a*m; bm=b*m; cm=c*m /*create some short-cut variable names.*/
 
do m=2; am=a*m; @.am.bm.cm=b*m;1 cm=c*m /*generatemark non-primitivesPythagorean Pythagoreans.triangle as a triple*/
if am+bm+cm>N then leave end /*is this multiple Pythagorean triple? m*/
trips=trips+1 end /*Eureka! We found a Pythagorean triplec*/
@.am.bm.cm=1 end /*mark Pythagorean triangle as a tripleb*/
end /*ma*/
end /*c*/
end /*b*/
end /*a*/
 
_=left('', 7) /*for padding the output with 7 blanks.*/
say 'max perimeter =' N _ "Pythagorean triples =" trips T _ 'primitives =' prims P
/*stick a fork in it, we're all done. */</lang>
'''output''' &nbsp; is identical to the 1<sup>st</sup> REXX version.