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; @.= !.; idx= 1; $= /*initialize some variables. */
!.= 0; @.= !.; idx= 1; $= /*initialize some variables. */
do #=1
do #=1
do j=1; if @.j then iterate /*J in list of coprime triplets? Skip.*/
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 #<3 then leave /*First two entries not defined? Use it*/
if gcd(j has(#-1) )\==1 then iterate /*is J coprime with last number? */
a= # - 1; b= # - 2 /*get the last two indices of sequence.*/
if gcd(j has(#-2) )\==1 then iterate /* " " " " penultimate number?*/
if gcd(j, !.a)\==1 then iterate /*J not coprime with last number?*/
leave /*OK, we've found a new coprime triplet*/
if gcd(j, !.b)\==1 then iterate /*" " " " penultimate " */
leave /*OK, we've found a new coprime triplet*/
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 j>=n then leave /*Have we exceeded the limit? Then quit*/
if cols==0 then iterate /*Not showing the numbers? Keep looking*/
@.j= 1; !.#= j /*flag a coprime triplet (two methods).*/
$= $ right( commas(j), w) /*append coprime triplet to output list*/
if cols==0 then iterate /*Not showing the numbers? Keep looking*/
if # // cols \== 0 then iterate /*Is output line full? No, keep looking*/
$= $ right( commas(j), w) /*append coprime triplet to output list*/
say center(idx, 7)'│' substr($, 2); $= /*show output line of coprime triplets.*/
if #//cols\==0 then iterate /*Is output line full? No, keep looking*/
idx= idx + cols /*bump the index for the output line. */
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 y; do until _==0; _= x//y; x= y; y= _; end; return 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=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>