Jump to content

Coprimes: Difference between revisions

2,312 bytes added ,  3 years ago
→‎{{header|REXX}}: added the computer programming language REXX.
(Added AppleScript.)
(→‎{{header|REXX}}: added the computer programming language REXX.)
Line 79:
[60, 15]
[21, 22, 25, 31, 143] Coprime</pre>
 
=={{header|REXX}}==
<lang rexx>/*REXX program tests pairs of numbers (separated by a comma) to see if they're comprime.*/
parse arg @
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. */
stuff= translate( word(@, j), , ',') /*change all commas (,) to blanks for GCD.*/
cofactor= gcd(stuff) /*get GCD (greatest common divisor) of #'s.*/
if cofactor==1 then say stuff " ─────── are coprime ───────"
else say stuff " have a cofactor of: " cofactor
end /*j*/
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
gcd: procedure; $= /*╔═══════════════════════════════════════╗*/
do #=1 for arg(); $= $ arg(#) /*║ This GCD can handle multiple arguments║*/
end /*#*/ /*║ and multiple numbers per argument, and║*/
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>
21,15 have a cofactor of: 3
 
17,23 ─────── are coprime ───────
 
36,12 have a cofactor of: 12
 
18,29 ─────── are coprime ───────
 
60,15 have a cofactor of: 15
 
21,22,25,143 ─────── are coprime ───────
 
-2,0 have a cofactor of: 2
 
0,-3 have a cofactor of: 3
</pre>
 
=={{header|Ring}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.