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 program tests pairs of numbers (separated by a comma) to see if they're comprime.*/
<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 each of the sets of numbers. */
do j=1 for words(@); say /*process each of the sets of numbers. */
stuff= translate( word(@, j), , ',') /*change all commas (,) to blanks for GCD.*/
stuff= translate( word(@, j), , ',') /*change commas (,) to blanks for GCD.*/
cofactor= gcd(stuff) /*get GCD (greatest common divisor) of #'s.*/
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 #=1 for arg(); $= $ arg(#) /*║ This GCD can handle multiple arguments║*/
do #=2 for arg()-1; $= $ arg(#) /*║This GCD handles multiple arguments║*/
end /*#*/ /*║ and multiple numbers per argument, and║*/
end /*#*/ /*║ & multiple numbers per argument, &║*/
parse var $ x z . /*║ negative numbers and non-integers. ║*/
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*/