Pythagorean triples: Difference between revisions

m
→‎{{header|REXX}}: changed some comments and indentations, added some comments.
m (→‎version 2: optimized a bit, added/changed some comments and indentation.)
m (→‎{{header|REXX}}: changed some comments and indentations, added some comments.)
Line 2,467:
<lang rexx>/*REXX pgm counts number of Pythagorean triples that exist given a max */
/* perimeter of N, and also counts how many of them are primitives.*/
trips=0; prims=0 /*zero # of triples, primatives. */
parse arg N .; if N=='' then n=100 /*get "N". If none, then assume.*/
 
do a=3 to N%3; aa=a*a /*limit side to 1/3 of perimeter.*/
 
do b=a+1 /*triangle can't be isosceles. */
ab=a+b /*compute partial perimeter. */
if ab>=nN then iterate a /*a+b>perimeterb≥perimeter? Try different A*/
aabb=aa+b*b /*compute sum of a² + b² (cheat)*/
 
do c=b+1; cc=c*c /*3rd side: also compute c² */
if ab+c>nN then iterate a /*a+b+c > perimeter? Try diff A.*/
cc=c*c if cc>aabb then iterate b /*ccompute C². > a²+b² ? Try different B.*/
if cc\== > aabb then iterate b /*c² ¬=> a²+b² ? Try different CB.*/
if tripscc\=trips+1 =aabb then iterate /*eureka. ¬= a²+b² ? Try different C.*/
primstrips=primstrips+(gcd(a,b)==1) /*iseureka. this We triple found a prim primitive? triple*/
prims=prims+(gcd(a,b)==1) end /*is this triple a primitive? */
end end /*ba*/
end end /*cb*/
end /*c*/
 
say 'max perimeter =' N, /*show a single line of output. */
left('',7) "Pythagorean triples =" trips, /*left('',07)≡7 blanks.*/
left('',7) 'primitives =' prims
exit /*stick a fork in it, we're done.*/