Is the word coprime?
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
Given a word, treat every letter as it's number in English alphabet (so a
becomes 1, b
becomes 2, z
becomes 26 and so on), and check if all of them, including duplicates, are pairwise coprime.
The input is exactly one word of lowercase English letters. The output is the fact if the word is coprime. Standard loopholes are forbidden.
Test cases:
man
:True
led
:False
(l=12
andd=4
havegcd=4
)mana
:True
(thougha
occurs multiple times, 1 and 1 are coprimes)mom
:False
(gcd(13,13)=13)
)a
:True
(if no pairs of letters, treat the word as coprime too)
This is a code-golf, so the shortest code in bytes wins!
code-golf number-theory primes
add a comment |Â
up vote
2
down vote
favorite
Given a word, treat every letter as it's number in English alphabet (so a
becomes 1, b
becomes 2, z
becomes 26 and so on), and check if all of them, including duplicates, are pairwise coprime.
The input is exactly one word of lowercase English letters. The output is the fact if the word is coprime. Standard loopholes are forbidden.
Test cases:
man
:True
led
:False
(l=12
andd=4
havegcd=4
)mana
:True
(thougha
occurs multiple times, 1 and 1 are coprimes)mom
:False
(gcd(13,13)=13)
)a
:True
(if no pairs of letters, treat the word as coprime too)
This is a code-golf, so the shortest code in bytes wins!
code-golf number-theory primes
Can we output0
if they are coprime and1
if not?
â dylnan
1 hour ago
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Given a word, treat every letter as it's number in English alphabet (so a
becomes 1, b
becomes 2, z
becomes 26 and so on), and check if all of them, including duplicates, are pairwise coprime.
The input is exactly one word of lowercase English letters. The output is the fact if the word is coprime. Standard loopholes are forbidden.
Test cases:
man
:True
led
:False
(l=12
andd=4
havegcd=4
)mana
:True
(thougha
occurs multiple times, 1 and 1 are coprimes)mom
:False
(gcd(13,13)=13)
)a
:True
(if no pairs of letters, treat the word as coprime too)
This is a code-golf, so the shortest code in bytes wins!
code-golf number-theory primes
Given a word, treat every letter as it's number in English alphabet (so a
becomes 1, b
becomes 2, z
becomes 26 and so on), and check if all of them, including duplicates, are pairwise coprime.
The input is exactly one word of lowercase English letters. The output is the fact if the word is coprime. Standard loopholes are forbidden.
Test cases:
man
:True
led
:False
(l=12
andd=4
havegcd=4
)mana
:True
(thougha
occurs multiple times, 1 and 1 are coprimes)mom
:False
(gcd(13,13)=13)
)a
:True
(if no pairs of letters, treat the word as coprime too)
This is a code-golf, so the shortest code in bytes wins!
code-golf number-theory primes
code-golf number-theory primes
edited 1 hour ago
0 '
3,32011227
3,32011227
asked 1 hour ago
bodqhrohro
39525
39525
Can we output0
if they are coprime and1
if not?
â dylnan
1 hour ago
add a comment |Â
Can we output0
if they are coprime and1
if not?
â dylnan
1 hour ago
Can we output
0
if they are coprime and 1
if not?â dylnan
1 hour ago
Can we output
0
if they are coprime and 1
if not?â dylnan
1 hour ago
add a comment |Â
5 Answers
5
active
oldest
votes
up vote
2
down vote
Wolfram Language (Mathematica), 36 bytes
CoprimeQ@@LetterNumber@Characters@#&
Try it online!
CoprimeQ@@(ToCharacterCode@#-96)&
is three bytes shorter.
â Misha Lavrov
7 secs ago
add a comment |Â
up vote
0
down vote
Python 2 - 122 bytes
This is honestly terrible, but I've spent way too long to not post this.
from fractions import*
def f(n):r=reduce;n=[ord(i)-96 for i in n];return r(lambda x,y:x*y/gcd(x,y),n)==r(lambda x,y:x*y,n)
Try it Online
add a comment |Â
up vote
0
down vote
Jelly, 11 bytes
Ã
ÂcO_96g/â‰ÂÂẸ
Try it online!
This is assuming we can output 0
if they are coprime and 1
if not. If this is not allowed (waiting to hear from OP) then just add a C
at the end of the code.
Ã
ÂcO_96g/â‰ÂÂẸ
Ã
Âc All pairs of characters without replacement
O Code point of each character
_96 Subtract 96. a->1, b->2, etc.
⬠For each pair:
g/ Get the greatest common denominator
â Subtract one from each gcd. If they are all 1 then this will give a list of 0s
Ẹ "Any". Gives 1 if there are any positive values in it (there are noncoprimes
in the list) and 0 if there are no positive values (all pairs are coprime)
add a comment |Â
up vote
0
down vote
Perl 6, 34 bytes
([lcm] @_)==[*] @_(.ords X-96)
Try it online!
If the lowest common multiple of the letters is equal to the product of the letters than they share no common divisors. An anonymous codeblock that takes a string and returns True or False.
Explanation:
# Anonymous code block
(.ords X-97) # Convert the letters to a list of numbers
# Apply it to
([lcm] @_) # The list reduced by the lcm
== # Is equal to?
[*] @_ # The list reduced by multiplication
add a comment |Â
up vote
0
down vote
q, 121 bytes
$[1=count x;:1b;r:l:$[0~y;:x;.z.s[y;x mod y]][y;]each x;2>count l where l<>1[x;]each x;(sum r)=count x[1+.Q.a?x]]
add a comment |Â
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
Wolfram Language (Mathematica), 36 bytes
CoprimeQ@@LetterNumber@Characters@#&
Try it online!
CoprimeQ@@(ToCharacterCode@#-96)&
is three bytes shorter.
â Misha Lavrov
7 secs ago
add a comment |Â
up vote
2
down vote
Wolfram Language (Mathematica), 36 bytes
CoprimeQ@@LetterNumber@Characters@#&
Try it online!
CoprimeQ@@(ToCharacterCode@#-96)&
is three bytes shorter.
â Misha Lavrov
7 secs ago
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Wolfram Language (Mathematica), 36 bytes
CoprimeQ@@LetterNumber@Characters@#&
Try it online!
Wolfram Language (Mathematica), 36 bytes
CoprimeQ@@LetterNumber@Characters@#&
Try it online!
answered 1 hour ago
Jonathan Frech
5,96311039
5,96311039
CoprimeQ@@(ToCharacterCode@#-96)&
is three bytes shorter.
â Misha Lavrov
7 secs ago
add a comment |Â
CoprimeQ@@(ToCharacterCode@#-96)&
is three bytes shorter.
â Misha Lavrov
7 secs ago
CoprimeQ@@(ToCharacterCode@#-96)&
is three bytes shorter.â Misha Lavrov
7 secs ago
CoprimeQ@@(ToCharacterCode@#-96)&
is three bytes shorter.â Misha Lavrov
7 secs ago
add a comment |Â
up vote
0
down vote
Python 2 - 122 bytes
This is honestly terrible, but I've spent way too long to not post this.
from fractions import*
def f(n):r=reduce;n=[ord(i)-96 for i in n];return r(lambda x,y:x*y/gcd(x,y),n)==r(lambda x,y:x*y,n)
Try it Online
add a comment |Â
up vote
0
down vote
Python 2 - 122 bytes
This is honestly terrible, but I've spent way too long to not post this.
from fractions import*
def f(n):r=reduce;n=[ord(i)-96 for i in n];return r(lambda x,y:x*y/gcd(x,y),n)==r(lambda x,y:x*y,n)
Try it Online
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Python 2 - 122 bytes
This is honestly terrible, but I've spent way too long to not post this.
from fractions import*
def f(n):r=reduce;n=[ord(i)-96 for i in n];return r(lambda x,y:x*y/gcd(x,y),n)==r(lambda x,y:x*y,n)
Try it Online
Python 2 - 122 bytes
This is honestly terrible, but I've spent way too long to not post this.
from fractions import*
def f(n):r=reduce;n=[ord(i)-96 for i in n];return r(lambda x,y:x*y/gcd(x,y),n)==r(lambda x,y:x*y,n)
Try it Online
answered 1 hour ago
Rushabh Mehta
577221
577221
add a comment |Â
add a comment |Â
up vote
0
down vote
Jelly, 11 bytes
Ã
ÂcO_96g/â‰ÂÂẸ
Try it online!
This is assuming we can output 0
if they are coprime and 1
if not. If this is not allowed (waiting to hear from OP) then just add a C
at the end of the code.
Ã
ÂcO_96g/â‰ÂÂẸ
Ã
Âc All pairs of characters without replacement
O Code point of each character
_96 Subtract 96. a->1, b->2, etc.
⬠For each pair:
g/ Get the greatest common denominator
â Subtract one from each gcd. If they are all 1 then this will give a list of 0s
Ẹ "Any". Gives 1 if there are any positive values in it (there are noncoprimes
in the list) and 0 if there are no positive values (all pairs are coprime)
add a comment |Â
up vote
0
down vote
Jelly, 11 bytes
Ã
ÂcO_96g/â‰ÂÂẸ
Try it online!
This is assuming we can output 0
if they are coprime and 1
if not. If this is not allowed (waiting to hear from OP) then just add a C
at the end of the code.
Ã
ÂcO_96g/â‰ÂÂẸ
Ã
Âc All pairs of characters without replacement
O Code point of each character
_96 Subtract 96. a->1, b->2, etc.
⬠For each pair:
g/ Get the greatest common denominator
â Subtract one from each gcd. If they are all 1 then this will give a list of 0s
Ẹ "Any". Gives 1 if there are any positive values in it (there are noncoprimes
in the list) and 0 if there are no positive values (all pairs are coprime)
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Jelly, 11 bytes
Ã
ÂcO_96g/â‰ÂÂẸ
Try it online!
This is assuming we can output 0
if they are coprime and 1
if not. If this is not allowed (waiting to hear from OP) then just add a C
at the end of the code.
Ã
ÂcO_96g/â‰ÂÂẸ
Ã
Âc All pairs of characters without replacement
O Code point of each character
_96 Subtract 96. a->1, b->2, etc.
⬠For each pair:
g/ Get the greatest common denominator
â Subtract one from each gcd. If they are all 1 then this will give a list of 0s
Ẹ "Any". Gives 1 if there are any positive values in it (there are noncoprimes
in the list) and 0 if there are no positive values (all pairs are coprime)
Jelly, 11 bytes
Ã
ÂcO_96g/â‰ÂÂẸ
Try it online!
This is assuming we can output 0
if they are coprime and 1
if not. If this is not allowed (waiting to hear from OP) then just add a C
at the end of the code.
Ã
ÂcO_96g/â‰ÂÂẸ
Ã
Âc All pairs of characters without replacement
O Code point of each character
_96 Subtract 96. a->1, b->2, etc.
⬠For each pair:
g/ Get the greatest common denominator
â Subtract one from each gcd. If they are all 1 then this will give a list of 0s
Ẹ "Any". Gives 1 if there are any positive values in it (there are noncoprimes
in the list) and 0 if there are no positive values (all pairs are coprime)
answered 58 mins ago
dylnan
3,8182528
3,8182528
add a comment |Â
add a comment |Â
up vote
0
down vote
Perl 6, 34 bytes
([lcm] @_)==[*] @_(.ords X-96)
Try it online!
If the lowest common multiple of the letters is equal to the product of the letters than they share no common divisors. An anonymous codeblock that takes a string and returns True or False.
Explanation:
# Anonymous code block
(.ords X-97) # Convert the letters to a list of numbers
# Apply it to
([lcm] @_) # The list reduced by the lcm
== # Is equal to?
[*] @_ # The list reduced by multiplication
add a comment |Â
up vote
0
down vote
Perl 6, 34 bytes
([lcm] @_)==[*] @_(.ords X-96)
Try it online!
If the lowest common multiple of the letters is equal to the product of the letters than they share no common divisors. An anonymous codeblock that takes a string and returns True or False.
Explanation:
# Anonymous code block
(.ords X-97) # Convert the letters to a list of numbers
# Apply it to
([lcm] @_) # The list reduced by the lcm
== # Is equal to?
[*] @_ # The list reduced by multiplication
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Perl 6, 34 bytes
([lcm] @_)==[*] @_(.ords X-96)
Try it online!
If the lowest common multiple of the letters is equal to the product of the letters than they share no common divisors. An anonymous codeblock that takes a string and returns True or False.
Explanation:
# Anonymous code block
(.ords X-97) # Convert the letters to a list of numbers
# Apply it to
([lcm] @_) # The list reduced by the lcm
== # Is equal to?
[*] @_ # The list reduced by multiplication
Perl 6, 34 bytes
([lcm] @_)==[*] @_(.ords X-96)
Try it online!
If the lowest common multiple of the letters is equal to the product of the letters than they share no common divisors. An anonymous codeblock that takes a string and returns True or False.
Explanation:
# Anonymous code block
(.ords X-97) # Convert the letters to a list of numbers
# Apply it to
([lcm] @_) # The list reduced by the lcm
== # Is equal to?
[*] @_ # The list reduced by multiplication
edited 50 mins ago
answered 59 mins ago
Jo King
16k24189
16k24189
add a comment |Â
add a comment |Â
up vote
0
down vote
q, 121 bytes
$[1=count x;:1b;r:l:$[0~y;:x;.z.s[y;x mod y]][y;]each x;2>count l where l<>1[x;]each x;(sum r)=count x[1+.Q.a?x]]
add a comment |Â
up vote
0
down vote
q, 121 bytes
$[1=count x;:1b;r:l:$[0~y;:x;.z.s[y;x mod y]][y;]each x;2>count l where l<>1[x;]each x;(sum r)=count x[1+.Q.a?x]]
add a comment |Â
up vote
0
down vote
up vote
0
down vote
q, 121 bytes
$[1=count x;:1b;r:l:$[0~y;:x;.z.s[y;x mod y]][y;]each x;2>count l where l<>1[x;]each x;(sum r)=count x[1+.Q.a?x]]
q, 121 bytes
$[1=count x;:1b;r:l:$[0~y;:x;.z.s[y;x mod y]][y;]each x;2>count l where l<>1[x;]each x;(sum r)=count x[1+.Q.a?x]]
answered 14 mins ago
Thaufeki
314
314
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f172858%2fis-the-word-coprime%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Can we output
0
if they are coprime and1
if not?â dylnan
1 hour ago