Coprime triplets: Difference between revisions
Content added Content deleted
(→{{header|REXX}}: optimized and simplified the code.) |
m (→{{header|REXX}}: simplified 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; @.= !.; |
!.= 0; @.= !.; idx= 1; $= /*initialize some variables. */ |
||
do #=1 |
do #=1 |
||
do j=1; |
do j=1; if @.j then iterate /*J in list of coprime triplets? Skip.*/ |
||
if #<3 then leave |
if #<3 then leave /*First two entries not defined? Use it*/ |
||
a= # - 1; b= # - 2 /*get the last two indices of sequence.*/ |
|||
if gcd(j |
if gcd(j, !.a)\==1 then iterate /*J not coprime with last number?*/ |
||
if gcd(j, !.b)\==1 then iterate /*" " " " penultimate " */ |
|||
⚫ | |||
end /*j*/ |
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*/ |
end /*forever*/ |
||
if $\=='' then say center(idx, 7)'│' substr($, 2) /*show any residual output numbers*/ |
if $\=='' then say center(idx, 7)'│' substr($, 2) /*show any residual output numbers*/ |
||
Line 138: | Line 139: | ||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
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 |
gcd: procedure; parse arg x,y; do until _==0; _= x//y; x= y; y= _; end; return x</lang> |
||
has: parse arg _; return !._</lang> |
|||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |