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