Coprimes: Difference between revisions
Content added Content deleted
(Frink) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 10: | Line 10: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">F coprime(a, b) |
||
R gcd(a, b) == 1 |
R gcd(a, b) == 1 |
||
Line 17: | Line 17: | ||
(36, 12), |
(36, 12), |
||
(18, 29), |
(18, 29), |
||
(60, 15)].filter((x, y) -> coprime(x, y)))</ |
(60, 15)].filter((x, y) -> coprime(x, y)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 25: | Line 25: | ||
=={{header|8080 Assembly}}== |
=={{header|8080 Assembly}}== |
||
< |
<syntaxhighlight lang="8080asm">puts: equ 9 |
||
org 100h |
org 100h |
||
lxi h,pairs |
lxi h,pairs |
||
Line 87: | Line 87: | ||
db '***' ; Number output buffer |
db '***' ; Number output buffer |
||
nbuf: db ' $' |
nbuf: db ' $' |
||
nl: db 13,10,'$'</ |
nl: db 13,10,'$'</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>17 23 |
<pre>17 23 |
||
Line 93: | Line 93: | ||
=={{header|8086 Assembly}}== |
=={{header|8086 Assembly}}== |
||
< |
<syntaxhighlight lang="asm">puts: equ 9 ; MS-DOS syscall to print a string |
||
cpu 8086 |
cpu 8086 |
||
org 100h |
org 100h |
||
Line 143: | Line 143: | ||
db 18,29 |
db 18,29 |
||
db 60,15 |
db 60,15 |
||
dw 0</ |
dw 0</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>17 23 |
<pre>17 23 |
||
18 29</pre> |
18 29</pre> |
||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang="action!">INT FUNC Gcd(INT a,b) |
||
INT tmp |
INT tmp |
||
Line 179: | Line 179: | ||
Test(18,29) |
Test(18,29) |
||
Test(60,15) |
Test(60,15) |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Coprimes.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Coprimes.png Screenshot from Atari 8-bit computer] |
||
Line 191: | Line 191: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
< |
<syntaxhighlight lang="algol68">BEGIN # test the coprime-ness of some number pairs # |
||
# iterative Greatest Common Divisor routine, returns the gcd of m and n # |
# iterative Greatest Common Divisor routine, returns the gcd of m and n # |
||
PROC gcd = ( INT m, n )INT: |
PROC gcd = ( INT m, n )INT: |
||
Line 214: | Line 214: | ||
FI |
FI |
||
OD |
OD |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 223: | Line 223: | ||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
{{Trans|MAD}} |
{{Trans|MAD}} |
||
< |
<syntaxhighlight lang="algolw">BEGIN % check whether sme numbers are coPrime (their gcd is 1) or not % |
||
LOGICAL PROCEDURE COPRM ( INTEGER VALUE X, Y ) ; GCD( X, Y ) = 1; |
LOGICAL PROCEDURE COPRM ( INTEGER VALUE X, Y ) ; GCD( X, Y ) = 1; |
||
INTEGER PROCEDURE GCD ( INTEGER VALUE A, B ) ; |
INTEGER PROCEDURE GCD ( INTEGER VALUE A, B ) ; |
||
Line 247: | Line 247: | ||
IF COPRM( PP, QQ ) THEN WRITE( I_W := 4, S_W := 0, PP, QQ ) |
IF COPRM( PP, QQ ) THEN WRITE( I_W := 4, S_W := 0, PP, QQ ) |
||
END FOR_I |
END FOR_I |
||
END.</ |
END.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 257: | Line 257: | ||
=={{header|APL}}== |
=={{header|APL}}== |
||
{{works with|Dyalog APL}} |
{{works with|Dyalog APL}} |
||
< |
<syntaxhighlight lang="apl">(⊢(/⍨)1=∨/¨) (21 15)(17 23)(36 12)(18 29)(60 15)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>┌─────┬─────┐ |
<pre>┌─────┬─────┐ |
||
Line 264: | Line 264: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
< |
<syntaxhighlight lang="applescript">on hcf(a, b) |
||
repeat until (b = 0) |
repeat until (b = 0) |
||
set x to a |
set x to a |
||
Line 282: | Line 282: | ||
if (hcf(p, q) is 1) then set end of coprimes to thisPair's contents |
if (hcf(p, q) is 1) then set end of coprimes to thisPair's contents |
||
end repeat |
end repeat |
||
return coprimes</ |
return coprimes</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
< |
<syntaxhighlight lang="applescript">{{17, 23}, {18, 29}}</syntaxhighlight> |
||
or, composing a definition and test from more general functions: |
or, composing a definition and test from more general functions: |
||
< |
<syntaxhighlight lang="applescript">------------------------- COPRIME ------------------------ |
||
-- coprime :: Int -> Int -> Bool |
-- coprime :: Int -> Int -> Bool |
||
Line 365: | Line 365: | ||
end script |
end script |
||
end if |
end if |
||
end mReturn</ |
end mReturn</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>{{17, 23}, {18, 29}}</pre> |
<pre>{{17, 23}, {18, 29}}</pre> |
||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">coprimes?: function [a b] -> 1 = gcd @[a b] |
||
loop [[21 15] [17 23] [36 12] [18 29] [60 15]] 'pair [ |
loop [[21 15] [17 23] [36 12] [18 29] [60 15]] 'pair [ |
||
print [pair\0 "and" pair\1 "ara" (coprimes? pair\0 pair\1)? -> "coprimes." -> "not coprimes."] |
print [pair\0 "and" pair\1 "ara" (coprimes? pair\0 pair\1)? -> "coprimes." -> "not coprimes."] |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 385: | Line 385: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f COPRIMES.AWK |
# syntax: GAWK -f COPRIMES.AWK |
||
BEGIN { |
BEGIN { |
||
Line 402: | Line 402: | ||
return(q?gcd(q,(p%q)):p) |
return(q?gcd(q,(p%q)):p) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 410: | Line 410: | ||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
< |
<syntaxhighlight lang="basic">10 DEFINT A-Z |
||
20 READ N |
20 READ N |
||
30 FOR I=1 TO N |
30 FOR I=1 TO N |
||
Line 424: | Line 424: | ||
130 DATA 36,12 |
130 DATA 36,12 |
||
140 DATA 18,29 |
140 DATA 18,29 |
||
150 DATA 60,15</ |
150 DATA 60,15</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 17 23 |
<pre> 17 23 |
||
Line 430: | Line 430: | ||
=={{header|BCPL}}== |
=={{header|BCPL}}== |
||
< |
<syntaxhighlight lang="bcpl">get "libhdr" |
||
let gcd(a,b) = b=0 -> a, gcd(b, a rem b) |
let gcd(a,b) = b=0 -> a, gcd(b, a rem b) |
||
Line 442: | Line 442: | ||
for i=0 to n-1 |
for i=0 to n-1 |
||
if coprime(ps!i, qs!i) do writef("%N %N*N", ps!i, qs!i) |
if coprime(ps!i, qs!i) do writef("%N %N*N", ps!i, qs!i) |
||
$)</ |
$)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>17 23 |
<pre>17 23 |
||
Line 448: | Line 448: | ||
=={{header|BQN}}== |
=={{header|BQN}}== |
||
< |
<syntaxhighlight lang="bqn">GCD ← {𝕨(|𝕊⍟(>⟜0)⊣)𝕩} |
||
SelectCoprimes ← (1=GCD´¨)⊸/ |
SelectCoprimes ← (1=GCD´¨)⊸/ |
||
SelectCoprimes ⟨21‿15,17‿23,36‿12,18‿29,60‿15⟩</ |
SelectCoprimes ⟨21‿15,17‿23,36‿12,18‿29,60‿15⟩</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>⟨ ⟨ 17 23 ⟩ ⟨ 18 29 ⟩ ⟩</pre> |
<pre>⟨ ⟨ 17 23 ⟩ ⟨ 18 29 ⟩ ⟩</pre> |
||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
int gcd(int a, int b) { |
int gcd(int a, int b) { |
||
Line 487: | Line 487: | ||
} |
} |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>{17, 23} |
<pre>{17, 23} |
||
Line 493: | Line 493: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <algorithm> |
#include <algorithm> |
||
#include <vector> |
#include <vector> |
||
Line 532: | Line 532: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>{17, 23} |
<pre>{17, 23} |
||
Line 538: | Line 538: | ||
=={{header|Cowgol}}== |
=={{header|Cowgol}}== |
||
< |
<syntaxhighlight lang="cowgol">include "cowgol.coh"; |
||
sub gcd(a: uint8, b: uint8): (r: uint8) is |
sub gcd(a: uint8, b: uint8): (r: uint8) is |
||
Line 571: | Line 571: | ||
end if; |
end if; |
||
i := i + 1; |
i := i + 1; |
||
end loop;</ |
end loop;</syntaxhighlight> |
||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Coprimes. Nigel Galloway: May 4th., 2021 |
// Coprimes. Nigel Galloway: May 4th., 2021 |
||
let rec fN g=function 0->g=1 |n->fN n (g%n) |
let rec fN g=function 0->g=1 |n->fN n (g%n) |
||
[(21,15);(17,23);(36,12);(18,29);(60,15)] |> List.filter(fun(n,g)->fN n g)|>List.iter(fun(n,g)->printfn "%d and %d are coprime" n g) |
[(21,15);(17,23);(36,12);(18,29);(60,15)] |> List.filter(fun(n,g)->fN n g)|>List.iter(fun(n,g)->printfn "%d and %d are coprime" n g) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 586: | Line 586: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
{{works with|Factor|0.98}} |
{{works with|Factor|0.98}} |
||
< |
<syntaxhighlight lang="factor">USING: io kernel math prettyprint sequences ; |
||
: coprime? ( seq -- ? ) [ ] [ simple-gcd ] map-reduce 1 = ; |
: coprime? ( seq -- ? ) [ ] [ simple-gcd ] map-reduce 1 = ; |
||
Line 598: | Line 598: | ||
{ 21 22 25 31 143 } |
{ 21 22 25 31 143 } |
||
} |
} |
||
[ dup pprint coprime? [ " Coprime" write ] when nl ] each</ |
[ dup pprint coprime? [ " Coprime" write ] when nl ] each</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 610: | Line 610: | ||
=={{header|Fermat}}== |
=={{header|Fermat}}== |
||
< |
<syntaxhighlight lang="fermat">Func Is_coprime(a, b) = if GCD(a,b)=1 then 1 else 0 fi.</syntaxhighlight> |
||
=={{header|FOCAL}}== |
=={{header|FOCAL}}== |
||
< |
<syntaxhighlight lang="focal">01.10 S P(1)=21; S Q(1)=15 |
||
01.20 S P(2)=17; S Q(2)=23 |
01.20 S P(2)=17; S Q(2)=23 |
||
01.30 S P(3)=36; S Q(3)=12 |
01.30 S P(3)=36; S Q(3)=12 |
||
Line 633: | Line 633: | ||
03.40 I (A-1)3.6,3.5,3.6 |
03.40 I (A-1)3.6,3.5,3.6 |
||
03.50 T %4,P(N),Q(N),! |
03.50 T %4,P(N),Q(N),! |
||
03.60 R</ |
03.60 R</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>= 17= 23 |
<pre>= 17= 23 |
||
Line 639: | Line 639: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">function gcdp( a as uinteger, b as uinteger ) as uinteger |
||
'returns the gcd of two positive integers |
'returns the gcd of two positive integers |
||
if b = 0 then return a |
if b = 0 then return a |
||
Line 659: | Line 659: | ||
print is_coprime(18,29) |
print is_coprime(18,29) |
||
print is_coprime(60,15) |
print is_coprime(60,15) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}}<pre> |
{{out}}<pre> |
||
false |
false |
||
Line 669: | Line 669: | ||
=={{header|Frink}}== |
=={{header|Frink}}== |
||
< |
<syntaxhighlight lang="frink">pairs = [ [21,15],[17,23],[36,12],[18,29],[60,15] ] |
||
for [a,b] = pairs |
for [a,b] = pairs |
||
println["[$a, $b] are " + (gcd[a,b] == 1 ? "" : "not ") + "coprime"]</ |
println["[$a, $b] are " + (gcd[a,b] == 1 ? "" : "not ") + "coprime"]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 684: | Line 684: | ||
{{libheader|Go-rcu}} |
{{libheader|Go-rcu}} |
||
Uses the same observation as the Wren entry. |
Uses the same observation as the Wren entry. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 699: | Line 699: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 709: | Line 709: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">------------------------- COPRIMES ----------------------- |
||
coprime :: Integral a => a -> a -> Bool |
coprime :: Integral a => a -> a -> Bool |
||
Line 726: | Line 726: | ||
(18, 29), |
(18, 29), |
||
(60, 15) |
(60, 15) |
||
]</ |
]</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>[(17,23),(18,29)]</pre> |
<pre>[(17,23),(18,29)]</pre> |
||
Line 732: | Line 732: | ||
=={{header|J}}== |
=={{header|J}}== |
||
< |
<syntaxhighlight lang="j">([#~1=+./"1) >21 15;17 23;36 12;18 29;60 15</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>17 23 |
<pre>17 23 |
||
Line 740: | Line 740: | ||
{{works with|jq}} |
{{works with|jq}} |
||
'''Works with gojq, the Go implementation of jq''' |
'''Works with gojq, the Go implementation of jq''' |
||
<syntaxhighlight lang="jq"> |
|||
<lang jq> |
|||
# Note that jq optimizes the recursive call of _gcd in the following: |
# Note that jq optimizes the recursive call of _gcd in the following: |
||
def gcd(a;b): |
def gcd(a;b): |
||
Line 749: | Line 749: | ||
# Input: an array |
# Input: an array |
||
def coprime: gcd(.[0]; .[1]) == 1; |
def coprime: gcd(.[0]; .[1]) == 1; |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''The task''' |
'''The task''' |
||
< |
<syntaxhighlight lang="jq">"The following pairs of numbers are coprime:", |
||
([[21,15],[17,23],[36,12],[18,29],[60,15]][] |
([[21,15],[17,23],[36,12],[18,29],[60,15]][] |
||
| select(coprime)) |
| select(coprime)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 763: | Line 763: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">filter(p -> gcd(p...) == 1, [[21,15],[17,23],[36,12],[18,29],[60,15],[21,22,25,31,143]]) |
||
</ |
</syntaxhighlight>{{out}}<pre> |
||
3-element Vector{Vector{Int64}}: |
3-element Vector{Vector{Int64}}: |
||
[17, 23] |
[17, 23] |
||
Line 772: | Line 772: | ||
=={{header|MAD}}== |
=={{header|MAD}}== |
||
< |
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER |
||
INTERNAL FUNCTION COPRM.(X,Y) = GCD.(X,Y).E.1 |
INTERNAL FUNCTION COPRM.(X,Y) = GCD.(X,Y).E.1 |
||
Line 797: | Line 797: | ||
VECTOR VALUES FMT = $I4,I4*$ |
VECTOR VALUES FMT = $I4,I4*$ |
||
END OF PROGRAM </ |
END OF PROGRAM </syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>COPRIMES |
<pre>COPRIMES |
||
Line 804: | Line 804: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">CoprimeQ @@@ {{21, 15}, {17, 23}, {36, 12}, {18, 29}, {60, 15}}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>{False, True, False, True, False}</pre> |
<pre>{False, True, False, True, False}</pre> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import math |
||
for (a, b) in [(21, 15), (17, 23), (36, 12), (18, 29), (60, 15)]: |
for (a, b) in [(21, 15), (17, 23), (36, 12), (18, 29), (60, 15)]: |
||
echo a, " and ", b, " are ", if gcd(a, b) == 1: "coprimes." else: "not coprimes."</ |
echo a, " and ", b, " are ", if gcd(a, b) == 1: "coprimes." else: "not coprimes."</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 823: | Line 823: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{libheader|ntheory}} |
{{libheader|ntheory}} |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use ntheory 'gcd'; |
use ntheory 'gcd'; |
||
Line 829: | Line 829: | ||
printf "%7s %s\n", (gcd(@$_) == 1 ? 'Coprime' : ''), join ', ', @$_ |
printf "%7s %s\n", (gcd(@$_) == 1 ? 'Coprime' : ''), join ', ', @$_ |
||
for [21,15], [17,23], [36,12], [18,29], [60,15], [21,22,25,31,143]; |
for [21,15], [17,23], [36,12], [18,29], [60,15], [21,22,25,31,143]; |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> 21, 15 |
<pre> 21, 15 |
||
Line 839: | Line 839: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">function</span> <span style="color: #000000;">gcd1</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">gcd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span> |
<span style="color: #008080;">function</span> <span style="color: #000000;">gcd1</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">gcd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span> |
||
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">({{</span><span style="color: #000000;">21</span><span style="color: #0000FF;">,</span><span style="color: #000000;">15</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">17</span><span style="color: #0000FF;">,</span><span style="color: #000000;">23</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">36</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">18</span><span style="color: #0000FF;">,</span><span style="color: #000000;">29</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">60</span><span style="color: #0000FF;">,</span><span style="color: #000000;">15</span><span style="color: #0000FF;">}},</span><span style="color: #000000;">gcd1</span><span style="color: #0000FF;">)</span> |
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">({{</span><span style="color: #000000;">21</span><span style="color: #0000FF;">,</span><span style="color: #000000;">15</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">17</span><span style="color: #0000FF;">,</span><span style="color: #000000;">23</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">36</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">18</span><span style="color: #0000FF;">,</span><span style="color: #000000;">29</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">60</span><span style="color: #0000FF;">,</span><span style="color: #000000;">15</span><span style="color: #0000FF;">}},</span><span style="color: #000000;">gcd1</span><span style="color: #0000FF;">)</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 848: | Line 848: | ||
</pre> |
</pre> |
||
A longer set/element such as {21,22,25,30,143} would also be shown as coprime, since it is, albeit not pairwise coprime - for the latter you would need something like: |
A longer set/element such as {21,22,25,30,143} would also be shown as coprime, since it is, albeit not pairwise coprime - for the latter you would need something like: |
||
<!--< |
<!--<syntaxhighlight lang="phix">--> |
||
<span style="color: #008080;">function</span> <span style="color: #000000;">pairwise_coprime</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> |
<span style="color: #008080;">function</span> <span style="color: #000000;">pairwise_coprime</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> |
||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span> |
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span> |
||
Line 858: | Line 858: | ||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span> |
||
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">({{</span><span style="color: #000000;">21</span><span style="color: #0000FF;">,</span><span style="color: #000000;">15</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">17</span><span style="color: #0000FF;">,</span><span style="color: #000000;">23</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">36</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">18</span><span style="color: #0000FF;">,</span><span style="color: #000000;">29</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">60</span><span style="color: #0000FF;">,</span><span style="color: #000000;">15</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">21</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">22</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">25</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">31</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">143</span><span style="color: #0000FF;">}},</span><span style="color: #000000;">pairwise_coprime</span><span style="color: #0000FF;">)</span> |
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">({{</span><span style="color: #000000;">21</span><span style="color: #0000FF;">,</span><span style="color: #000000;">15</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">17</span><span style="color: #0000FF;">,</span><span style="color: #000000;">23</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">36</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">18</span><span style="color: #0000FF;">,</span><span style="color: #000000;">29</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">60</span><span style="color: #0000FF;">,</span><span style="color: #000000;">15</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">21</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">22</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">25</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">31</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">143</span><span style="color: #0000FF;">}},</span><span style="color: #000000;">pairwise_coprime</span><span style="color: #0000FF;">)</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
Output is the same as the above, because this excludes the {21, 22, 25, 31, 143}, since both 22 and 143 are divisible by 11. |
Output is the same as the above, because this excludes the {21, 22, 25, 31, 143}, since both 22 and 143 are divisible by 11. |
||
=={{header|PL/M}}== |
=={{header|PL/M}}== |
||
< |
<syntaxhighlight lang="plm">100H: |
||
BDOS: PROCEDURE (FN, ARG); |
BDOS: PROCEDURE (FN, ARG); |
||
DECLARE FN BYTE, ARG ADDRESS; |
DECLARE FN BYTE, ARG ADDRESS; |
||
Line 911: | Line 911: | ||
END; |
END; |
||
CALL BDOS(0,0); |
CALL BDOS(0,0); |
||
EOF</ |
EOF</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>17 23 |
<pre>17 23 |
||
Line 917: | Line 917: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python">'''Coprimes''' |
||
from math import gcd |
from math import gcd |
||
Line 945: | Line 945: | ||
# MAIN --- |
# MAIN --- |
||
if __name__ == '__main__': |
if __name__ == '__main__': |
||
main()</ |
main()</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>[(17, 23), (18, 29)]</pre> |
<pre>[(17, 23), (18, 29)]</pre> |
||
Line 954: | Line 954: | ||
<code>gcd</code> is defined at [[Greatest common divisor#Quackery]]. |
<code>gcd</code> is defined at [[Greatest common divisor#Quackery]]. |
||
< |
<syntaxhighlight lang="quackery"> [ gcd 1 = ] is coprime ( n n --> b ) |
||
' [ [ 21 15 ] |
' [ [ 21 15 ] |
||
Line 968: | Line 968: | ||
coprime not if |
coprime not if |
||
[ say " not" ] |
[ say " not" ] |
||
say " coprime." cr ]</ |
say " coprime." cr ]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 980: | Line 980: | ||
=={{header|R}}== |
=={{header|R}}== |
||
< |
<syntaxhighlight lang="rsplus">factors <- function(n) c(Filter(function(x) n %% x == 0, seq_len(n %/% 2)), n) |
||
isCoprime <- function(p, q) all(intersect(factors(p), factors(q)) == 1) |
isCoprime <- function(p, q) all(intersect(factors(p), factors(q)) == 1) |
||
output <- data.frame(p = c(21, 17, 36, 18, 60), q = c(15, 23, 12, 29, 15)) |
output <- data.frame(p = c(21, 17, 36, 18, 60), q = c(15, 23, 12, 29, 15)) |
||
print(transform(output, "Coprime" = ifelse(mapply(isCoprime, p, q), "Yes", "No")))</ |
print(transform(output, "Coprime" = ifelse(mapply(isCoprime, p, q), "Yes", "No")))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> p q Coprime |
<pre> p q Coprime |
||
Line 996: | Line 996: | ||
There is a coprime? function in the math/number-theory library to show off (more useful if you're using typed racket). |
There is a coprime? function in the math/number-theory library to show off (more useful if you're using typed racket). |
||
< |
<syntaxhighlight lang="racket">#lang racket/base |
||
;; Rename only necessary so we can distinguish it |
;; Rename only necessary so we can distinguish it |
||
Line 1,015: | Line 1,015: | ||
(list number-theory/coprime? |
(list number-theory/coprime? |
||
gcd/coprime? |
gcd/coprime? |
||
(λ ns (= 1 (apply gcd ns))))))</ |
(λ ns (= 1 (apply gcd ns))))))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,044: | Line 1,044: | ||
How do you determine if numbers are co-prime? Check to see if the [[Greatest common divisor]] is equal to one. Since we're duplicating tasks willy-nilly, lift code from [[Greatest_common_divisor#Raku|that task]], (or in this case, just use the builtin). |
How do you determine if numbers are co-prime? Check to see if the [[Greatest common divisor]] is equal to one. Since we're duplicating tasks willy-nilly, lift code from [[Greatest_common_divisor#Raku|that task]], (or in this case, just use the builtin). |
||
<lang |
<syntaxhighlight lang="raku" line>say .raku, ( [gcd] |$_ ) == 1 ?? ' Coprime' !! '' for [21,15],[17,23],[36,12],[18,29],[60,15],[21,22,25,31,143]</syntaxhighlight> |
||
<pre>[21, 15] |
<pre>[21, 15] |
||
[17, 23] Coprime |
[17, 23] Coprime |
||
Line 1,053: | Line 1,053: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
< |
<syntaxhighlight 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 @ /*obtain optional arguments from the CL*/ |
||
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' |
||
Line 1,074: | Line 1,074: | ||
end /*until*/ |
end /*until*/ |
||
end /*j*/ |
end /*j*/ |
||
return x</ |
return x</syntaxhighlight> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
Line 1,095: | Line 1,095: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
see "working..." + nl |
see "working..." + nl |
||
row = 0 |
row = 0 |
||
Line 1,125: | Line 1,125: | ||
see "Found " + row + " coprimes" + nl |
see "Found " + row + " coprimes" + nl |
||
see "done..." + nl |
see "done..." + nl |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,138: | Line 1,138: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">pairs = [[21,15],[17,23],[36,12],[18,29],[60,15]] |
||
pairs.select{|p, q| p.gcd(q) == 1}.each{|pair| p pair} |
pairs.select{|p, q| p.gcd(q) == 1}.each{|pair| p pair} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>[17, 23] |
<pre>[17, 23] |
||
Line 1,147: | Line 1,147: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">var pairs = [[21,15],[17,23],[36,12],[18,29],[60,15]] |
||
say "The following pairs of numbers are coprime:" |
say "The following pairs of numbers are coprime:" |
||
pairs.grep { .gcd == 1 }.each { .say }</ |
pairs.grep { .gcd == 1 }.each { .say }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,160: | Line 1,160: | ||
{{libheader|Wren-math}} |
{{libheader|Wren-math}} |
||
Two numbers are coprime if their GCD is 1. |
Two numbers are coprime if their GCD is 1. |
||
< |
<syntaxhighlight lang="ecmascript">import "/math" for Int |
||
var pairs = [[21,15],[17,23],[36,12],[18,29],[60,15]] |
var pairs = [[21,15],[17,23],[36,12],[18,29],[60,15]] |
||
System.print("The following pairs of numbers are coprime:") |
System.print("The following pairs of numbers are coprime:") |
||
for (pair in pairs) if (Int.gcd(pair[0], pair[1]) == 1) System.print(pair)</ |
for (pair in pairs) if (Int.gcd(pair[0], pair[1]) == 1) System.print(pair)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,174: | Line 1,174: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">func GCD(A, B); \Return greatest common divisor of A and B |
||
int A, B; |
int A, B; |
||
[while A#B do |
[while A#B do |
||
Line 1,189: | Line 1,189: | ||
[IntOut(0, A); ChOut(0, ^,); IntOut(0, B); CrLf(0)]; |
[IntOut(0, A); ChOut(0, ^,); IntOut(0, B); CrLf(0)]; |
||
]; |
]; |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |