Coprime triplets: Difference between revisions

→‎{{header|REXX}}: optimized and simplified the code.
(→‎{{header|REXX}}: added the computer programming language REXX.)
(→‎{{header|REXX}}: optimized and simplified the code.)
Line 114:
if cols>0 then say ' index │'center(@copt, 1 + cols*(w+1) )
if cols>0 then say '───────┼'center("" , 1 + cols*(W+1), '─')
$!.=0; @.= !.; idx= 1; $ $= /*initialize some variables. */
do #=1
do j=1; if @.j\==. then iterate /*J in list of coprime triplets? Skip.*/
if has(#-1)==.|has(#-2)==.<3 then leave /*1stFirst two entries not define? Use it. */
if gcd(j has(#-1) )\==1 then iterate /*is J coprime with last number? */
if gcd(j has(#-2) )\==1 then iterate /* " " " " penultimate number?*/
leave /*OK, we've found a new coprime triplet*/
end /*j*/
if j>=n then leave /*Have we exceeded the limit? Then quit*/
@.j= j1; $= $ j !.#= j /*flag a coprime triplet; add(two to $ listmethods).*/
if cols==0 then iterate /*Not showing the numbers? Keep looking*/
$$= $$ right( commas(j), w) /*append coprime triplet to output list*/
if # // cols \== 0 then iterate then iterate /*Is output line full? No, keep looking*/
say center(idx, 7)'│' substr($$, 2); $ $= /*show output line of coprime triplets.*/
idx= idx + cols /*bump the index for the output line. */
end /*forever*/
 
if $$\=='' then say center(idx, 7)'│' substr($$, 2) /*show any residual output numbers*/
if cols>0 then say '───────┴'center("" , 1 + cols*(w+1), '─')
say
Line 138:
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?
hasgcd: procedure expose $; parse arg _x y; do until _==0; if _<1= x//y; then returnx= .y; elsey= return_; word($, _) end; return x
has: parse vararg _; $ x z . /*║negative numbersreturn and non-integers!. ║*_</lang>
/*──────────────────────────────────────────────────────────────────────────────────────*/
gcd: procedure; parse arg $ /*╔═══════════════════════════════════╗*/
do #=2 for arg()-1; $= $ arg(#) /*║This GCD handles multiple arguments║*/
end /*#*/ /*║ & multiple numbers per argument, &║*/
parse var $ x z . /*║negative numbers and non-integers. ║*/
if x=0 then x= z; x= abs(x) /*╚═══════════════════════════════════╝*/
do j=2 to words($); y= abs( word($, j) ); if y=0 then iterate
do until _==0; _= x // y; x= y; y= _
end /*until*/
end /*j*/
return x</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>