Pythagorean triples: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added more whitespace in output, used different form on "n". -- ~~~~)
m (→‎{{header|REXX}}: added another sample output for 1,000. -- ~~~~)
Line 1,610: Line 1,610:
prims=0
prims=0


do a=3 to N%3; aa=a*a /*limit side to 1/3 of perimeter.*/
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. */
do b=a+1 /*triangle can't be isosceles. */
ab=a+b /*compute partial perimeter. */
ab=a+b /*compute partial perimeter. */
if ab>=n then iterate a /*a+b>perimeter? Try different A*/
if ab>=n then iterate a /*a+b>perimeter? Try different A*/
aabb=aa+b*b /*compute sum of a² + b² (cheat)*/
aabb=aa+b*b /*compute sum of a² + b² (cheat)*/
do c=b+1; cc=c*c /*3rd side: also compute c² */
do c=b+1; cc=c*c /*3rd side: also compute c² */
if ab+c>n then iterate a /*a+b+c > perimeter? Try diff A.*/
if ab+c>n then iterate a /*a+b+c > perimeter? Try diff A.*/
if cc>aabb then iterate b /*c² > a²+b² ? Try different B.*/
if cc>aabb then iterate b /*c² > a²+b² ? Try different B.*/
if aabb\==cc then iterate /*c² ¬= a²+b² ? Try different C.*/
if cc\==aabb then iterate /*c² ¬= a²+b² ? Try different C.*/
trips=trips+1 /*eureka. */
trips=trips+1 /*eureka. */
prims=prims+(gcd(a,b)==1) /*is this triple a primative ? */
prims=prims+(gcd(a,b)==1) /*is this triple a primative ? */
end /*a*/
end /*a*/
end /*b*/
end /*b*/
end /*c*/
end /*c*/


say 'max perimeter = 'N, /*show a single line of output. */
say 'max perimeter = 'N, /*show a single line of output. */
left('',9) "Pythagorean triples =" trips, /*left('',0) ≡ 9 blanks.*/
left('',7) "Pythagorean triples =" trips, /*left('',0) ≡ 7 blanks.*/
left('',9) 'primatives =' prims
left('',7) 'primatives =' prims
exit
exit
/*──────────────────────────────────GCD subroutine──────────────────────*/
/*──────────────────────────────────GCD subroutine──────────────────────*/
gcd: procedure; arg x,y; do until _==0; _=x//y; x=y; y=_; end; return x</lang>
gcd: procedure; arg x,y; do until _==0; _=x//y; x=y; y=_; end; return x</lang>
'''output''' when using the input of: <tt> 100 </tt>
'''output''' when using the default input of 100
<pre style="overflow:scroll">
<pre style="overflow:scroll">
max perimeter = 100 Pythagorean triples = 17 primatives = 7
max perimeter = 100 Pythagorean triples = 17 primatives = 7
</pre>
'''output''' when using the input of: <tt> 1000 </tt>
<pre style="overflow:scroll">
max perimeter = 1000 Pythagorean triples = 325 primatives = 70
</pre>
</pre>