Special pythagorean triplet: Difference between revisions

m
→‎{{header|REXX}}: further optimized the DO loops.
m (→‎{{header|REXX}}: changed a word in the REXX section header.)
m (→‎{{header|REXX}}: further optimized the DO loops.)
Line 278:
Also, there were multiple shortcuts to limit an otherwise exhaustive search;   Once a sum or a square was too big,
<br>the next integer was used.
<lang rexx>/*REXX pgm computes/shows integers A, B, and C that solve: 0<A<B<C; A+B+C = 1000; A^2+B^2 = C^2 */
parse arg s hi . /*obtain optional argument from the CL.*/
if s=='' | s=="," then s= 1000 /*Not specified? Then use the default.*/
if hi=='' | hi=="," then hi= 1000 /* " " " " " " */
 
do j=1 for hi; @.j= j*j /*precompute squares up to HI. */
end /*j*/
hi2= hi-2
 
do a=1 for hi-2hi2; aa= @.a /*go hunting for solutions to equation.equations*/
do b=a+1 tofor hihi2-1a; ab= a + b /*calculate sum of two (A,B) squares.*/
if ab>hi then iterate a /*Sum of A+B>HI? Then stop with B's */
aabb= aa + @.b /*compute the sum of: A^2 + B^2 */
do c=b+1 to hi for hi2-b /*handle alltest intsintegers that satisfy equationequations.*/
if @.c > aabb then iterate b /*Square> A^2+B^2? Then stop with C's.*/
if @.c \== aabb then iterate /*Square\=A^2+B^2? Then keep searching*/
abc= ab + c /*compute the sum of A + B + C */
if abc == s then leave a /*Does A+B+C = S? Then solution found*/
if abc > s then iterate b /* " " > S? Then stop with C's.*/
end /*c*/
end /*b*/
end /*a*/
say ' a=' a " b=" b ' c=' c
 
say ' a=' a " b=" b ' c=' c
exit 0 /*stick a fork in it, we're all done. */</lang>
{{out|output|text=&nbsp; when using the default inputs:}}