Coprime triplets: Difference between revisions

→‎{{header|REXX}}: added the computer programming language REXX.
m (→‎{{header|Raku}}: minor efficiency tweaks)
(→‎{{header|REXX}}: added the computer programming language REXX.)
Line 104:
688 1305 1369 692 1311 1373 694 1317 1375 698
1323 1381 704 1329 1379 706</pre>
 
=={{header|REXX}}==
<lang rexx>/*REXX program finds and display coprime triplets below a specified limit (limit=50).*/
parse arg n cols . /*obtain optional arguments from the CL*/
if n=='' | n=="," then n= 50 /*Not specified? Then use the default.*/
if cols=='' | cols=="," then cols= 10 /* " " " " " " */
w= length( commas(n) ) + 1 /*width of a number in any column. */
@copt= ' coprime triplets where N < ' commas(n)
if cols>0 then say ' index │'center(@copt, 1 + cols*(w+1) )
if cols>0 then say '───────┼'center("" , 1 + cols*(W+1), '─')
$=; @.=.; 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)==. then leave /*1st 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= j; $= $ j /*flag a coprime triplet; add to $ list*/
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 $$\=='' then say center(idx, 7)'│' substr($$, 2) /*show any residual output numbers*/
if cols>0 then say '───────┴'center("" , 1 + cols*(w+1), '─')
say
say 'Found ' commas(#-1) @copt
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
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 $ /*╔═══════════════════════════════════╗*/
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>
index │ coprime triplets where N < 50
───────┼─────────────────────────────────────────
1 │ 1 2 3 5 4 7 9 8 11 13
11 │ 6 17 19 10 21 23 16 15 29 14
21 │ 25 27 22 31 35 12 37 41 18 43
31 │ 47 20 33 49 26 45
───────┴─────────────────────────────────────────
 
Found 36 coprime triplets where N < 50
</pre>
 
=={{header|Ring}}==