Pythagorean triples: Difference between revisions

Content added Content deleted
(→‎{{header|REXX}}: added separator line for subroutine; made REXX code compliant, added whitespace. -- ~~~~)
m (→‎{{header|REXX}}: added more whitespace in output, used different form on "n". -- ~~~~)
Line 1,603:
=={{header|REXX}}==
A brute force approach.
<lang rexx>/*REXX pgm countscountse number of Pythagorean triples that exist given a maximum max */
/* perimeter of N, and also count how many of them are primatives. */
 
parse arg nN .; if nN=='' then n=100 /*get "N". If none, then assume.*/
trips=0
prims=0
 
do a=3 to nN%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>=n then iterate a /*a+b>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>n then iterate a /*a+b+c > perimeter? Try diff A.*/
if cc>aabb then iterate b /*c² > a²+b² ? Try different B.*/
if aabb\==cc then iterate /*c² ¬= a²+b² ? Try different C.*/
trips=trips+1 /*eureka. */
prims=prims+(gcd(a,b)==1) /*is thethis triple isa primative ? */
end /*a*/
end /*b*/
end /*c*/
 
say 'max perimeter = 'nN, " Pythagorean triples="trips ' primatives='prims /*show a single line of output. */
left('',9) "Pythagorean triples =" trips, /*left('',0) ≡ 9 blanks.*/
left('',9) 'primatives =' prims
exit
/*──────────────────────────────────GCD subroutine──────────────────────*/
/*------------------------------------GCD subroutine--------------------*/
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>
<pre style="overflow:scroll">
max perimeter = 100 Pythagorean triples = 17 primatives = 7
</pre>