Special pythagorean triplet: Difference between revisions

Content added Content deleted
(→‎{{header|Raku}}: return the product as well)
(→‎{{header|REXX}}: added an option to allow more than one solution to be found.)
Line 296: Line 296:
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 /* " " " " " " */

do j=1 for hi; @.j= j*j /*precompute squares up to HI. */
end /*j*/
hi2= hi-2
hi2= hi-2
do j=1 for hi; @.j= j*j /*precompute squares up to HI. */
end /*j*/
#= 0 /*#: the number of solutions found. */
do a=1 for hi2; aa= @.a /*go hunting for solutions to equations*/
do a=1 for hi2; aa= @.a /*go hunting for solutions to equations*/
do b=a+1 for hi2-a; ab= a + b /*calculate sum of two (A,B) squares.*/
do b=a+1 for hi2-a; ab= a + b /*calculate sum of two (A,B) squares.*/
Line 308: Line 308:
if @.c \== aabb then iterate /*Square\=A^2+B^2? Then keep searching*/
if @.c \== aabb then iterate /*Square\=A^2+B^2? Then keep searching*/
abc= ab + c /*compute the sum of A + B + C */
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 call show /*Does A+B+C = S? Then solution found*/
if abc > s then iterate b /* " " > S? Then stop with C's.*/
if abc > s then iterate b /* " " > S? Then stop with C's.*/
end /*c*/
end /*c*/
end /*b*/
end /*b*/
end /*a*/
end /*a*/
say # ' solutions found.'
say ' a=' a " b=" b ' c=' c
exit 0 /*stick a fork in it, we're all done. */</lang>
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: #= # + 1; say ' a=' a " b=" b ' c=' c; exit 0
/*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>