Jump to content

Special pythagorean triplet: Difference between revisions

→‎{{header|REXX}}: added an option to allow more than one solution to be found.
(→‎{{header|Raku}}: return the product as well)
(→‎{{header|REXX}}: added an option to allow more than one solution to be found.)
Line 296:
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 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 b=a+1 for hi2-a; ab= a + b /*calculate sum of two (A,B) squares.*/
Line 308:
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 leavecall ashow /*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 # ' solutions found.'
say ' a=' a " b=" b ' c=' c
exit 0 /*stick a fork in it, we're all done. */</lang>
/*──────────────────────────────────────────────────────────────────────────────────────*/
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:}}
<pre>
Cookies help us deliver our services. By using our services, you agree to our use of cookies.