Coprime triplets: Difference between revisions
Content added Content deleted
(→{{header|REXX}}: added the computer programming language REXX.) |
(→{{header|REXX}}: optimized and simplified the code.) |
||
Line 114: | Line 114: | ||
if cols>0 then say ' index │'center(@copt, 1 + cols*(w+1) ) |
if cols>0 then say ' index │'center(@copt, 1 + cols*(w+1) ) |
||
if cols>0 then say '───────┼'center("" , 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 #<3 then leave /*First 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= 1; !.#= j /*flag a coprime triplet (two methods).*/ |
|||
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 /*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 |
if $\=='' then say center(idx, 7)'│' substr($, 2) /*show any residual output numbers*/ |
||
if cols>0 then say '───────┴'center("" , 1 + cols*(w+1), '─') |
if cols>0 then say '───────┴'center("" , 1 + cols*(w+1), '─') |
||
say |
say |
||
Line 138: | Line 138: | ||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ? |
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ? |
||
gcd: procedure; parse arg x y; do until _==0; _= x//y; x= y; y= _; end; return x |
|||
⚫ | |||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
gcd: procedure; parse arg $ /*╔═══════════════════════════════════╗*/ |
|||
do #=2 for arg()-1; $= $ arg(#) /*║This GCD handles multiple arguments║*/ |
|||
end /*#*/ /*║ & multiple numbers per argument, &║*/ |
|||
⚫ | |||
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= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |