Special pythagorean triplet: Difference between revisions

m
→‎{{header|REXX}}: incorporated optimizations for another Rosetta Code task: Pythagorean triples, added support to (only) find 1 (or any number) of solutions.
m (→‎{{header|REXX}}: incorporated optimizations for another Rosetta Code task: Pythagorean triples, added support to (only) find 1 (or any number) of solutions.)
Line 366:
<br>the next integer was used.
<lang rexx>/*REXX pgm computes integers A, B, C that solve: 0<A<B<C; A+B+C = 1000; A^2+B^2 = C^2 */
parse arg s hi n . /*obtain optional argument from the CL.*/
if s=='' | s=="," then s= 1000 /*Not specified? Then use the default.*/
if hi=='' | hi=="," then hi= 1000 /* " " " " " " */
if n=='' | n=="," then n= 1 /* " " " " " " */
hi2= hi-2
hi2= hi-2 do j=1 for hi; @.j= j*j /*precompute squares up to HI. /*N: number of solutions to find/show.*/
end / do j=1 for hi; @.j= j*j /*pre─compute squares ──► HI, inclusive*/
#= 0 end /*#: the number of solutions found. j*/
#= 0; do a=1 for hi2; aa= @.a pad= left('', 9) /*go#: hunting forthe number of solutions tofound. equations*/
do do a=2 by b=a+1 2 for hi2-a%2; ab aa= @.a + b /*calculatego sumhunting offor twosolutions to (A,B) squares.equations*/
do if ab>hib=a+1 for hi2-a; ab= a + then iterate a b /*Sumcalculate sum of two (A+,B>HI?) Then stop with B's squares.*/
if aabb= aa + @.b ab>hi then iterate a /*compute the sumSum of: A^2 + B^2>HI? Then stop with B's */
aabb= aa + @.b do c=b+1 for hi2-b /*testcompute the sum of: A^2 + B^2 integers that satisfy equations.*/
do c=b+1 iffor @.chi2-b > aabb then iterate b /*Square> A^2+B^2? Then stop with C's /*test integers that satisfy equations.*/
if @.c \== > aabb then iterate b /*Square\=> A^2+B^2? Then keepstop searchingwith C's.*/
if @.c abc\== ab + c aabb then iterate /*compute the sum of" \=A ^2+ B^2? + C Then keep searching*/
abc= ab + c if abc == s then call show /*Doescompute the sum of A + B+C = S?+ C Then solution found*/
if abc > == s then iteratecall bshow /* " /*Does A+B+C " >= S? Then stopsolution with C's.found*/
if abc > s then iterate b end /*cIs " > S? Then stop with C's.*/
end /*bc*/
end /*ab*/
end /*a*/
done: say pad pad pad # ' solutions found.'
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: #= # + 1; say pad ' a=' a pad " b=" b pad ' c=' c; if #>=n then signal done; exit 0return</lang>
/*replace EXIT 0 with RETURN to find more possible solutions──►──┘ */</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
a= 200 b= 375 c= 425
1 solutions found.
</pre>