Coprimes: Difference between revisions
Content added Content deleted
(→{{header|Haskell}}: Added a definition and example in Haskell) |
m (→{{header|REXX}}: simplified the function.) |
||
Line 265: | Line 265: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
<lang rexx>/*REXX |
<lang rexx>/*REXX prgm tests number sequences (min. of two #'s, separated by a commas) if comprime.*/ |
||
parse arg @ /*obtain optional arguments from the CL*/ |
|||
parse arg @ |
|||
if @='' | @=="," then @= '21,15 17,23 36,12 18,29 60,15 21,22,25,143 -2,0 0,-3' |
if @='' | @=="," then @= '21,15 17,23 36,12 18,29 60,15 21,22,25,143 -2,0 0,-3' |
||
do j=1 for words(@); say /*process |
do j=1 for words(@); say /*process each of the sets of numbers. */ |
||
stuff= translate( word(@, j), , ',') /*change |
stuff= translate( word(@, j), , ',') /*change commas (,) to blanks for GCD.*/ |
||
cofactor= gcd(stuff) /*get |
cofactor= gcd(stuff) /*get Greatest Common Divisor of #'s.*/ |
||
if cofactor==1 then say stuff " ─────── are coprime ───────" |
if cofactor==1 then say stuff " ─────── are coprime ───────" |
||
else say stuff " have a cofactor of: " cofactor |
else say stuff " have a cofactor of: " cofactor |
||
Line 277: | Line 277: | ||
exit 0 /*stick a fork in it, we're all done. */ |
exit 0 /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
gcd: procedure; |
gcd: procedure; parse arg $ /*╔═══════════════════════════════════╗*/ |
||
do #= |
do #=2 for arg()-1; $= $ arg(#) /*║This GCD handles multiple arguments║*/ |
||
end /*#*/ /*║ |
end /*#*/ /*║ & multiple numbers per argument, &║*/ |
||
parse var $ x z . /* |
parse var $ x z . /*║negative numbers and non-integers. ║*/ |
||
if x=0 then x= z; x= abs(x) /* |
if x=0 then x= z; x= abs(x) /*╚═══════════════════════════════════╝*/ |
||
do j=2 to words($); y= abs( word($, j) ); if y=0 then iterate |
do j=2 to words($); y= abs( word($, j) ); if y=0 then iterate |
||
do until _==0; _= x//y; x= y; y= _ |
do until _==0; _= x // y; x= y; y= _ |
||
end /*until*/ |
end /*until*/ |
||
end /*j*/ |
end /*j*/ |