Is the word coprime?

The name of the pictureThe name of the pictureThe name of the pictureClash 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 and d=4 have gcd=4)


  • mana: True (though a 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!










share|improve this question























  • Can we output 0 if they are coprime and 1 if not?
    – dylnan
    1 hour ago














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 and d=4 have gcd=4)


  • mana: True (though a 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!










share|improve this question























  • Can we output 0 if they are coprime and 1 if not?
    – dylnan
    1 hour ago












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 and d=4 have gcd=4)


  • mana: True (though a 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!










share|improve this question















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 and d=4 have gcd=4)


  • mana: True (though a 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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago









0 '

3,32011227




3,32011227










asked 1 hour ago









bodqhrohro

39525




39525











  • 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















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










5 Answers
5






active

oldest

votes

















up vote
2
down vote














Wolfram Language (Mathematica), 36 bytes



CoprimeQ@@LetterNumber@Characters@#&


Try it online!






share|improve this answer




















  • CoprimeQ@@(ToCharacterCode@#-96)& is three bytes shorter.
    – Misha Lavrov
    7 secs ago

















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






share|improve this answer



























    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)





    share|improve this answer



























      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





      share|improve this answer





























        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]]





        share|improve this answer




















          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "200"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          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






























          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!






          share|improve this answer




















          • CoprimeQ@@(ToCharacterCode@#-96)& is three bytes shorter.
            – Misha Lavrov
            7 secs ago














          up vote
          2
          down vote














          Wolfram Language (Mathematica), 36 bytes



          CoprimeQ@@LetterNumber@Characters@#&


          Try it online!






          share|improve this answer




















          • CoprimeQ@@(ToCharacterCode@#-96)& is three bytes shorter.
            – Misha Lavrov
            7 secs ago












          up vote
          2
          down vote










          up vote
          2
          down vote










          Wolfram Language (Mathematica), 36 bytes



          CoprimeQ@@LetterNumber@Characters@#&


          Try it online!






          share|improve this answer













          Wolfram Language (Mathematica), 36 bytes



          CoprimeQ@@LetterNumber@Characters@#&


          Try it online!







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 1 hour ago









          Jonathan Frech

          5,96311039




          5,96311039











          • 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




          CoprimeQ@@(ToCharacterCode@#-96)& is three bytes shorter.
          – Misha Lavrov
          7 secs ago










          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






          share|improve this answer
























            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






            share|improve this answer






















              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






              share|improve this answer












              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







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 1 hour ago









              Rushabh Mehta

              577221




              577221




















                  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)





                  share|improve this answer
























                    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)





                    share|improve this answer






















                      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)





                      share|improve this answer













                      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)






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 58 mins ago









                      dylnan

                      3,8182528




                      3,8182528




















                          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





                          share|improve this answer


























                            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





                            share|improve this answer
























                              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





                              share|improve this answer















                              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






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited 50 mins ago

























                              answered 59 mins ago









                              Jo King

                              16k24189




                              16k24189




















                                  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]]





                                  share|improve this answer
























                                    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]]





                                    share|improve this answer






















                                      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]]





                                      share|improve this answer












                                      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]]






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered 14 mins ago









                                      Thaufeki

                                      314




                                      314



























                                           

                                          draft saved


                                          draft discarded















































                                           


                                          draft saved


                                          draft discarded














                                          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













































































                                          Comments

                                          Popular posts from this blog

                                          What does second last employer means? [closed]

                                          Installing NextGIS Connect into QGIS 3?

                                          One-line joke