Special pythagorean triplet: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (→{{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 /* " " " " " " */ |
||
⚫ | |||
⚫ | |||
hi2= hi-2 |
hi2= hi-2 |
||
⚫ | |||
⚫ | |||
#= 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 |
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.' |
|||
⚫ | |||
exit 0 /*stick a fork in it, we're all done. */ |
exit 0 /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
⚫ | |||
/*replace EXIT 0 with RETURN to find more possible solutions─────┘ */</lang> |
|||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |