Special pythagorean triplet: Difference between revisions

Content added Content deleted
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: Line 366:
<br>the next integer was used.
<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 */
<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 . /*obtain optional argument from the CL.*/
parse arg s hi n . /*obtain optional argument from the CL.*/
if s=='' | s=="," then s= 1000 /*Not specified? Then use the default.*/
if s=='' | s=="," then s= 1000 /*Not specified? Then use the default.*/
if hi=='' | hi=="," then hi= 1000 /* " " " " " " */
if hi=='' | hi=="," then hi= 1000 /* " " " " " " */
if n=='' | n=="," then n= 1 /* " " " " " " */
hi2= hi-2
do j=1 for hi; @.j= j*j /*precompute squares up to HI. */
hi2= hi-2 /*N: number of solutions to find/show.*/
end /*j*/
do j=1 for hi; @.j= j*j /*pre─compute squares ──► HI, inclusive*/
#= 0 /*#: the number of solutions found. */
end /*j*/
do a=1 for hi2; aa= @.a /*go hunting for solutions to equations*/
#= 0; pad= left('', 9) /*#: the number of solutions found. */
do b=a+1 for hi2-a; ab= a + b /*calculate sum of two (A,B) squares.*/
do a=2 by 2 for hi2%2; aa= @.a /*go hunting for solutions to equations*/
if ab>hi then iterate a /*Sum of A+B>HI? Then stop with B's */
do b=a+1 for hi2-a; ab= a + b /*calculate sum of two (A,B) squares.*/
aabb= aa + @.b /*compute the sum of: A^2 + B^2 */
if ab>hi then iterate a /*Sum of A+B>HI? Then stop with B's */
do c=b+1 for hi2-b /*test integers that satisfy equations.*/
aabb= aa + @.b /*compute the sum of: A^2 + B^2 */
if @.c > aabb then iterate b /*Square> A^2+B^2? Then stop with C's.*/
do c=b+1 for hi2-b /*test integers that satisfy equations.*/
if @.c \== aabb then iterate /*Square\=A^2+B^2? Then keep searching*/
if @.c > aabb then iterate b /*Square> A^2+B^2? Then stop with C's.*/
abc= ab + c /*compute the sum of A + B + C */
if @.c \== aabb then iterate /* " \=A^2+B^2? Then keep searching*/
if abc == s then call show /*Does A+B+C = S? Then solution found*/
abc= ab + c /*compute the sum of A + B + C */
if abc > s then iterate b /* " " > S? Then stop with C's.*/
if abc == s then call show /*Does A+B+C = S? Then solution found*/
end /*c*/
if abc > s then iterate b /*Is " > S? Then stop with C's.*/
end /*b*/
end /*c*/
end /*a*/
end /*b*/
end /*a*/
say # ' solutions found.'
done: say pad pad pad # ' solutions found.'
exit 0 /*stick a fork in it, we're all done. */
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: #= # + 1; say ' a=' a " b=" b ' c=' c; exit 0
show: #= #+1; say pad 'a=' a pad "b=" b pad 'c=' c; if #>=n then signal done; return</lang>
/*replace EXIT 0 with RETURN to find more possible solutions──►──┘ */</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
a= 200 b= 375 c= 425
a= 200 b= 375 c= 425
1 solutions found.
</pre>
</pre>