Generate the k-ary necklaces of length n












6












$begingroup$


The set of necklaces is the set of strings, where two strings are considered to be the same necklace if you can rotate one into the other. Your program will take nonnegative integers k and n, and generate a list of the k-ary (fixed) necklaces of length n.



Necklaces will be represented by any representative string. So the necklace corresponding to the strings {ABC, BCA, CAB} can represented as ABC, BCA, or CAB.



The program will output a list of strings, such that each necklace is represented by exactly one string in the list. So for instance, outputting ABC and BCA would not be valid, since the same necklace was represented twice.



Some other details:




  • Your program may choose which k characters to use for the alphabet. If you prefer, you can instead choose k distinct values of any type, and have your program output lists (or some other sequence type) of those k values. (This might in fact be necessary if k is greater than the number of characters avaible in your language.) For example, if k=3, you could use {A,B,C}, {&, H, (}, or even {10, 11, 12} as your alphabet. The only restriction is that elements of your alphabet may not contain whitespace.

  • You may output any representative for each necklace. So for the necklace {ABC, BCA, CAB}, you may output ABC, BCA, or CAB. There is no "preferred" representative.


This is code-golf, so the shortest program wins!





Also, here is a useful test to see if your program is working. Given k and n, the list your program outputs have the length listed here. Here is an OEIS sequence corresponding to k=2.



Also, here are some examples and counterexamples. Note that these are not test cases, because any input has both an infinite number of correct and incorrect outputs. I will give inputs in the form (k,n).



Examples:





  • (2,2): [AA, BB, AB]


  • (2,2): [AA, BA, BB]


  • (2,2): [[1,0], [0,0], [1,1]]


  • (3,2): [AA, BB, CC, AB, BC, AC]


  • (2,3): [AAA, BBB, AAB, ABB]


  • (3,3): [AAA, BBB, CCC, AAB, AAC, ABB, ABC, ACB, ACC, BBC, BCC]


  • (0,n): (for positive integers n)


  • (k,0): [] (for nonnegative integers k)


  • (k,n): Search "necklaces with k colors and n beads" on wolfram alpha (for positive integers k and n)


Counterexamples:





  • (2,2): [AA, BB, AB, BA]


  • (2,2): [AA, BB]


  • (3,3): [AAA, BBB, CCC, AAB, AAC, ABB, ABC, ACC, BBC, BCC]


  • (3,3): [AAA, BBB, CCC, AAB, BAA, AAC, ABB, ABC, ACB, ACC, BBC, BCC]


  • (k,n): Any list whose length is different from this (for positive integers n)


  • (3,3): [AAA, BBB, CCC, AAB, BAA, AAC, ABB, ABC, ACC, BBC, BCC]










share|improve this question











$endgroup$












  • $begingroup$
    You could have a program that verifies outputs. I think examples would also be helpful.
    $endgroup$
    – lirtosiast
    Feb 1 at 19:27










  • $begingroup$
    @lirtosiast Well, that would also depend on the output format. Different languages have different notations for lists. I suppose I could make one for python and let people adapt it to different languages. Think that would help?
    $endgroup$
    – PyRulez
    Feb 1 at 19:39










  • $begingroup$
    Related
    $endgroup$
    – PyRulez
    Feb 1 at 19:44






  • 1




    $begingroup$
    Very closely related
    $endgroup$
    – Peter Taylor
    Feb 1 at 22:54










  • $begingroup$
    @PeterTaylor Indeed. One "gotcha" to look out for though is that the number of necklaces is not the same as the number of lyndon words, because a lyndon word also needs to be distinct from all its rotations.
    $endgroup$
    – PyRulez
    Feb 1 at 23:56
















6












$begingroup$


The set of necklaces is the set of strings, where two strings are considered to be the same necklace if you can rotate one into the other. Your program will take nonnegative integers k and n, and generate a list of the k-ary (fixed) necklaces of length n.



Necklaces will be represented by any representative string. So the necklace corresponding to the strings {ABC, BCA, CAB} can represented as ABC, BCA, or CAB.



The program will output a list of strings, such that each necklace is represented by exactly one string in the list. So for instance, outputting ABC and BCA would not be valid, since the same necklace was represented twice.



Some other details:




  • Your program may choose which k characters to use for the alphabet. If you prefer, you can instead choose k distinct values of any type, and have your program output lists (or some other sequence type) of those k values. (This might in fact be necessary if k is greater than the number of characters avaible in your language.) For example, if k=3, you could use {A,B,C}, {&, H, (}, or even {10, 11, 12} as your alphabet. The only restriction is that elements of your alphabet may not contain whitespace.

  • You may output any representative for each necklace. So for the necklace {ABC, BCA, CAB}, you may output ABC, BCA, or CAB. There is no "preferred" representative.


This is code-golf, so the shortest program wins!





Also, here is a useful test to see if your program is working. Given k and n, the list your program outputs have the length listed here. Here is an OEIS sequence corresponding to k=2.



Also, here are some examples and counterexamples. Note that these are not test cases, because any input has both an infinite number of correct and incorrect outputs. I will give inputs in the form (k,n).



Examples:





  • (2,2): [AA, BB, AB]


  • (2,2): [AA, BA, BB]


  • (2,2): [[1,0], [0,0], [1,1]]


  • (3,2): [AA, BB, CC, AB, BC, AC]


  • (2,3): [AAA, BBB, AAB, ABB]


  • (3,3): [AAA, BBB, CCC, AAB, AAC, ABB, ABC, ACB, ACC, BBC, BCC]


  • (0,n): (for positive integers n)


  • (k,0): [] (for nonnegative integers k)


  • (k,n): Search "necklaces with k colors and n beads" on wolfram alpha (for positive integers k and n)


Counterexamples:





  • (2,2): [AA, BB, AB, BA]


  • (2,2): [AA, BB]


  • (3,3): [AAA, BBB, CCC, AAB, AAC, ABB, ABC, ACC, BBC, BCC]


  • (3,3): [AAA, BBB, CCC, AAB, BAA, AAC, ABB, ABC, ACB, ACC, BBC, BCC]


  • (k,n): Any list whose length is different from this (for positive integers n)


  • (3,3): [AAA, BBB, CCC, AAB, BAA, AAC, ABB, ABC, ACC, BBC, BCC]










share|improve this question











$endgroup$












  • $begingroup$
    You could have a program that verifies outputs. I think examples would also be helpful.
    $endgroup$
    – lirtosiast
    Feb 1 at 19:27










  • $begingroup$
    @lirtosiast Well, that would also depend on the output format. Different languages have different notations for lists. I suppose I could make one for python and let people adapt it to different languages. Think that would help?
    $endgroup$
    – PyRulez
    Feb 1 at 19:39










  • $begingroup$
    Related
    $endgroup$
    – PyRulez
    Feb 1 at 19:44






  • 1




    $begingroup$
    Very closely related
    $endgroup$
    – Peter Taylor
    Feb 1 at 22:54










  • $begingroup$
    @PeterTaylor Indeed. One "gotcha" to look out for though is that the number of necklaces is not the same as the number of lyndon words, because a lyndon word also needs to be distinct from all its rotations.
    $endgroup$
    – PyRulez
    Feb 1 at 23:56














6












6








6





$begingroup$


The set of necklaces is the set of strings, where two strings are considered to be the same necklace if you can rotate one into the other. Your program will take nonnegative integers k and n, and generate a list of the k-ary (fixed) necklaces of length n.



Necklaces will be represented by any representative string. So the necklace corresponding to the strings {ABC, BCA, CAB} can represented as ABC, BCA, or CAB.



The program will output a list of strings, such that each necklace is represented by exactly one string in the list. So for instance, outputting ABC and BCA would not be valid, since the same necklace was represented twice.



Some other details:




  • Your program may choose which k characters to use for the alphabet. If you prefer, you can instead choose k distinct values of any type, and have your program output lists (or some other sequence type) of those k values. (This might in fact be necessary if k is greater than the number of characters avaible in your language.) For example, if k=3, you could use {A,B,C}, {&, H, (}, or even {10, 11, 12} as your alphabet. The only restriction is that elements of your alphabet may not contain whitespace.

  • You may output any representative for each necklace. So for the necklace {ABC, BCA, CAB}, you may output ABC, BCA, or CAB. There is no "preferred" representative.


This is code-golf, so the shortest program wins!





Also, here is a useful test to see if your program is working. Given k and n, the list your program outputs have the length listed here. Here is an OEIS sequence corresponding to k=2.



Also, here are some examples and counterexamples. Note that these are not test cases, because any input has both an infinite number of correct and incorrect outputs. I will give inputs in the form (k,n).



Examples:





  • (2,2): [AA, BB, AB]


  • (2,2): [AA, BA, BB]


  • (2,2): [[1,0], [0,0], [1,1]]


  • (3,2): [AA, BB, CC, AB, BC, AC]


  • (2,3): [AAA, BBB, AAB, ABB]


  • (3,3): [AAA, BBB, CCC, AAB, AAC, ABB, ABC, ACB, ACC, BBC, BCC]


  • (0,n): (for positive integers n)


  • (k,0): [] (for nonnegative integers k)


  • (k,n): Search "necklaces with k colors and n beads" on wolfram alpha (for positive integers k and n)


Counterexamples:





  • (2,2): [AA, BB, AB, BA]


  • (2,2): [AA, BB]


  • (3,3): [AAA, BBB, CCC, AAB, AAC, ABB, ABC, ACC, BBC, BCC]


  • (3,3): [AAA, BBB, CCC, AAB, BAA, AAC, ABB, ABC, ACB, ACC, BBC, BCC]


  • (k,n): Any list whose length is different from this (for positive integers n)


  • (3,3): [AAA, BBB, CCC, AAB, BAA, AAC, ABB, ABC, ACC, BBC, BCC]










share|improve this question











$endgroup$




The set of necklaces is the set of strings, where two strings are considered to be the same necklace if you can rotate one into the other. Your program will take nonnegative integers k and n, and generate a list of the k-ary (fixed) necklaces of length n.



Necklaces will be represented by any representative string. So the necklace corresponding to the strings {ABC, BCA, CAB} can represented as ABC, BCA, or CAB.



The program will output a list of strings, such that each necklace is represented by exactly one string in the list. So for instance, outputting ABC and BCA would not be valid, since the same necklace was represented twice.



Some other details:




  • Your program may choose which k characters to use for the alphabet. If you prefer, you can instead choose k distinct values of any type, and have your program output lists (or some other sequence type) of those k values. (This might in fact be necessary if k is greater than the number of characters avaible in your language.) For example, if k=3, you could use {A,B,C}, {&, H, (}, or even {10, 11, 12} as your alphabet. The only restriction is that elements of your alphabet may not contain whitespace.

  • You may output any representative for each necklace. So for the necklace {ABC, BCA, CAB}, you may output ABC, BCA, or CAB. There is no "preferred" representative.


This is code-golf, so the shortest program wins!





Also, here is a useful test to see if your program is working. Given k and n, the list your program outputs have the length listed here. Here is an OEIS sequence corresponding to k=2.



Also, here are some examples and counterexamples. Note that these are not test cases, because any input has both an infinite number of correct and incorrect outputs. I will give inputs in the form (k,n).



Examples:





  • (2,2): [AA, BB, AB]


  • (2,2): [AA, BA, BB]


  • (2,2): [[1,0], [0,0], [1,1]]


  • (3,2): [AA, BB, CC, AB, BC, AC]


  • (2,3): [AAA, BBB, AAB, ABB]


  • (3,3): [AAA, BBB, CCC, AAB, AAC, ABB, ABC, ACB, ACC, BBC, BCC]


  • (0,n): (for positive integers n)


  • (k,0): [] (for nonnegative integers k)


  • (k,n): Search "necklaces with k colors and n beads" on wolfram alpha (for positive integers k and n)


Counterexamples:





  • (2,2): [AA, BB, AB, BA]


  • (2,2): [AA, BB]


  • (3,3): [AAA, BBB, CCC, AAB, AAC, ABB, ABC, ACC, BBC, BCC]


  • (3,3): [AAA, BBB, CCC, AAB, BAA, AAC, ABB, ABC, ACB, ACC, BBC, BCC]


  • (k,n): Any list whose length is different from this (for positive integers n)


  • (3,3): [AAA, BBB, CCC, AAB, BAA, AAC, ABB, ABC, ACC, BBC, BCC]







code-golf string math combinatorics generation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 1 at 23:54







PyRulez

















asked Feb 1 at 19:09









PyRulezPyRulez

3,61442357




3,61442357












  • $begingroup$
    You could have a program that verifies outputs. I think examples would also be helpful.
    $endgroup$
    – lirtosiast
    Feb 1 at 19:27










  • $begingroup$
    @lirtosiast Well, that would also depend on the output format. Different languages have different notations for lists. I suppose I could make one for python and let people adapt it to different languages. Think that would help?
    $endgroup$
    – PyRulez
    Feb 1 at 19:39










  • $begingroup$
    Related
    $endgroup$
    – PyRulez
    Feb 1 at 19:44






  • 1




    $begingroup$
    Very closely related
    $endgroup$
    – Peter Taylor
    Feb 1 at 22:54










  • $begingroup$
    @PeterTaylor Indeed. One "gotcha" to look out for though is that the number of necklaces is not the same as the number of lyndon words, because a lyndon word also needs to be distinct from all its rotations.
    $endgroup$
    – PyRulez
    Feb 1 at 23:56


















  • $begingroup$
    You could have a program that verifies outputs. I think examples would also be helpful.
    $endgroup$
    – lirtosiast
    Feb 1 at 19:27










  • $begingroup$
    @lirtosiast Well, that would also depend on the output format. Different languages have different notations for lists. I suppose I could make one for python and let people adapt it to different languages. Think that would help?
    $endgroup$
    – PyRulez
    Feb 1 at 19:39










  • $begingroup$
    Related
    $endgroup$
    – PyRulez
    Feb 1 at 19:44






  • 1




    $begingroup$
    Very closely related
    $endgroup$
    – Peter Taylor
    Feb 1 at 22:54










  • $begingroup$
    @PeterTaylor Indeed. One "gotcha" to look out for though is that the number of necklaces is not the same as the number of lyndon words, because a lyndon word also needs to be distinct from all its rotations.
    $endgroup$
    – PyRulez
    Feb 1 at 23:56
















$begingroup$
You could have a program that verifies outputs. I think examples would also be helpful.
$endgroup$
– lirtosiast
Feb 1 at 19:27




$begingroup$
You could have a program that verifies outputs. I think examples would also be helpful.
$endgroup$
– lirtosiast
Feb 1 at 19:27












$begingroup$
@lirtosiast Well, that would also depend on the output format. Different languages have different notations for lists. I suppose I could make one for python and let people adapt it to different languages. Think that would help?
$endgroup$
– PyRulez
Feb 1 at 19:39




$begingroup$
@lirtosiast Well, that would also depend on the output format. Different languages have different notations for lists. I suppose I could make one for python and let people adapt it to different languages. Think that would help?
$endgroup$
– PyRulez
Feb 1 at 19:39












$begingroup$
Related
$endgroup$
– PyRulez
Feb 1 at 19:44




$begingroup$
Related
$endgroup$
– PyRulez
Feb 1 at 19:44




1




1




$begingroup$
Very closely related
$endgroup$
– Peter Taylor
Feb 1 at 22:54




$begingroup$
Very closely related
$endgroup$
– Peter Taylor
Feb 1 at 22:54












$begingroup$
@PeterTaylor Indeed. One "gotcha" to look out for though is that the number of necklaces is not the same as the number of lyndon words, because a lyndon word also needs to be distinct from all its rotations.
$endgroup$
– PyRulez
Feb 1 at 23:56




$begingroup$
@PeterTaylor Indeed. One "gotcha" to look out for though is that the number of necklaces is not the same as the number of lyndon words, because a lyndon word also needs to be distinct from all its rotations.
$endgroup$
– PyRulez
Feb 1 at 23:56










5 Answers
5






active

oldest

votes


















3












$begingroup$


Jelly, 8 bytes



ṗṙJṂȯƲ€Q


A dyadic Link accepting the alphabet size, k, on the left and the necklace length, n, on the right which yields a list of the necklaces using the positive integers up to k as the alphabet.



Try it online!



How?



ṗṙJṂȯƲ€Q - Link: integer k, integer n
ṗ - Cartesian power of [1,2,...,k] with n
- i.e. all n-length lists using alphabet [1,2,...,k]
Ʋ€ - last four links as a monad for €ach list, L:
J - range of length - i.e. [1,2,...,n]
ṙ - rotate left by each of those values - i.e. get all rotations
Ṃ - minimum
ȯ - logical OR with L (since the minimum of an empty list is 0 not )
Q - unique values the resulting list





share|improve this answer











$endgroup$













  • $begingroup$
    This appears to give the wrong answers if k or n are less than 2 (although I am not familiar enough with Jelly to know for sure).
    $endgroup$
    – PyRulez
    Feb 1 at 20:11












  • $begingroup$
    Fixed it to agree with given examples for a cost of 1 byte, are there any remaining issues?
    $endgroup$
    – Jonathan Allan
    Feb 1 at 20:48






  • 1




    $begingroup$
    I do not see any. The problems seemed related to empty and singleton lists, but those issues are now fixed.
    $endgroup$
    – PyRulez
    Feb 1 at 21:00



















1












$begingroup$

JavaScript (ES7),  123 116 115  113 bytes



Takes input as (k)(n). Each necklace is represented as an array of integers.





k=>g=(n,x=k**n,o=)=>x--?g(n,x,o.some(a=>~(a+[,a]).search(b),b=[...Array(n)].map(_=>x/k**--n%k|0))?o:[b,...o]):o


Try it online!






share|improve this answer











$endgroup$





















    1












    $begingroup$


    Python 2, 105 bytes





    lambda k,n:{min(i[j:]+i[:j]for j in range(n or 1))for i in product(*[range(k)]*n)}
    from itertools import*


    Try it online!



    Returns a set of tuples.






    share|improve this answer









    $endgroup$





















      0












      $begingroup$


      Japt, 27 bytes



      ?UÆVoÃrï ®c £ZéYÃn gÃâ:[]


      Try it online!



      Outputs with numbers starting from 0 for the values. Inputs in reversed order (n then k). The r throws a fit for n = 0 so I had to add an significant special-case.



      Explanation:



      ?                     :[]    #Special case: return [] if n is 0
      UÆ Ã #Get n rows of:
      Vo # The numbers [0..k-1]
      rï #Get all possible lists containing one number from each row...
      ® Ã #For each of those lists:
      c # Fix the formatting
      £ZéYÃ # Get every possible rotation
      n g # Choose the "minimum" one
      â #Remove duplicate lists





      share|improve this answer











      $endgroup$













      • $begingroup$
        On input (3,4), it seems to be missing a necklace corresponding to [3,2,1].
        $endgroup$
        – PyRulez
        Feb 1 at 23:51










      • $begingroup$
        @PyRulez Fixed, I think
        $endgroup$
        – Kamil Drakari
        Feb 2 at 0:59



















      0












      $begingroup$


      Charcoal, 37 bytes



      Nθ≔Xθ…⁰⊕Nη≔⊟ηζ¿ηIEΦζ⁼κ⌊﹪÷×ι⊕ζηζ﹪÷ιηθ¶


      Try it online! Link is to verbose version of code. Explanation:



      Nθ≔Xθ…⁰⊕Nη≔⊟ηζ


      Input k and n and calculate the powers of k from 1 to n, but then keep kⁿ separately.



      ¿η...¶


      If n is zero then just print a newline. (I could special-case 0 in the algorithm which would only cost two bytes but the resulting array only contains an empty array and Charcoal optimises that away on output, so you can't tell that the empty array is there.)



      Φζ⁼κ⌊﹪÷×ι⊕ζηζ


      For all the numbers from 0 to kⁿ, multiply them all by kⁿ+1, then divide by the above powers of k, then take the modulus of all of them by kⁿ. Keep only those numbers which equal the minimum of the resulting set.



      IE...﹪÷ιηθ


      Convert the numbers to reversed base k padded to length n for output using Charcoal's default output which is each element on its own line and each necklace double-spaced from the next necklace.






      share|improve this answer









      $endgroup$













        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',
        autoActivateHeartbeat: false,
        convertImagesToLinks: false,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        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%2f179386%2fgenerate-the-k-ary-necklaces-of-length-n%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        3












        $begingroup$


        Jelly, 8 bytes



        ṗṙJṂȯƲ€Q


        A dyadic Link accepting the alphabet size, k, on the left and the necklace length, n, on the right which yields a list of the necklaces using the positive integers up to k as the alphabet.



        Try it online!



        How?



        ṗṙJṂȯƲ€Q - Link: integer k, integer n
        ṗ - Cartesian power of [1,2,...,k] with n
        - i.e. all n-length lists using alphabet [1,2,...,k]
        Ʋ€ - last four links as a monad for €ach list, L:
        J - range of length - i.e. [1,2,...,n]
        ṙ - rotate left by each of those values - i.e. get all rotations
        Ṃ - minimum
        ȯ - logical OR with L (since the minimum of an empty list is 0 not )
        Q - unique values the resulting list





        share|improve this answer











        $endgroup$













        • $begingroup$
          This appears to give the wrong answers if k or n are less than 2 (although I am not familiar enough with Jelly to know for sure).
          $endgroup$
          – PyRulez
          Feb 1 at 20:11












        • $begingroup$
          Fixed it to agree with given examples for a cost of 1 byte, are there any remaining issues?
          $endgroup$
          – Jonathan Allan
          Feb 1 at 20:48






        • 1




          $begingroup$
          I do not see any. The problems seemed related to empty and singleton lists, but those issues are now fixed.
          $endgroup$
          – PyRulez
          Feb 1 at 21:00
















        3












        $begingroup$


        Jelly, 8 bytes



        ṗṙJṂȯƲ€Q


        A dyadic Link accepting the alphabet size, k, on the left and the necklace length, n, on the right which yields a list of the necklaces using the positive integers up to k as the alphabet.



        Try it online!



        How?



        ṗṙJṂȯƲ€Q - Link: integer k, integer n
        ṗ - Cartesian power of [1,2,...,k] with n
        - i.e. all n-length lists using alphabet [1,2,...,k]
        Ʋ€ - last four links as a monad for €ach list, L:
        J - range of length - i.e. [1,2,...,n]
        ṙ - rotate left by each of those values - i.e. get all rotations
        Ṃ - minimum
        ȯ - logical OR with L (since the minimum of an empty list is 0 not )
        Q - unique values the resulting list





        share|improve this answer











        $endgroup$













        • $begingroup$
          This appears to give the wrong answers if k or n are less than 2 (although I am not familiar enough with Jelly to know for sure).
          $endgroup$
          – PyRulez
          Feb 1 at 20:11












        • $begingroup$
          Fixed it to agree with given examples for a cost of 1 byte, are there any remaining issues?
          $endgroup$
          – Jonathan Allan
          Feb 1 at 20:48






        • 1




          $begingroup$
          I do not see any. The problems seemed related to empty and singleton lists, but those issues are now fixed.
          $endgroup$
          – PyRulez
          Feb 1 at 21:00














        3












        3








        3





        $begingroup$


        Jelly, 8 bytes



        ṗṙJṂȯƲ€Q


        A dyadic Link accepting the alphabet size, k, on the left and the necklace length, n, on the right which yields a list of the necklaces using the positive integers up to k as the alphabet.



        Try it online!



        How?



        ṗṙJṂȯƲ€Q - Link: integer k, integer n
        ṗ - Cartesian power of [1,2,...,k] with n
        - i.e. all n-length lists using alphabet [1,2,...,k]
        Ʋ€ - last four links as a monad for €ach list, L:
        J - range of length - i.e. [1,2,...,n]
        ṙ - rotate left by each of those values - i.e. get all rotations
        Ṃ - minimum
        ȯ - logical OR with L (since the minimum of an empty list is 0 not )
        Q - unique values the resulting list





        share|improve this answer











        $endgroup$




        Jelly, 8 bytes



        ṗṙJṂȯƲ€Q


        A dyadic Link accepting the alphabet size, k, on the left and the necklace length, n, on the right which yields a list of the necklaces using the positive integers up to k as the alphabet.



        Try it online!



        How?



        ṗṙJṂȯƲ€Q - Link: integer k, integer n
        ṗ - Cartesian power of [1,2,...,k] with n
        - i.e. all n-length lists using alphabet [1,2,...,k]
        Ʋ€ - last four links as a monad for €ach list, L:
        J - range of length - i.e. [1,2,...,n]
        ṙ - rotate left by each of those values - i.e. get all rotations
        Ṃ - minimum
        ȯ - logical OR with L (since the minimum of an empty list is 0 not )
        Q - unique values the resulting list






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Feb 1 at 20:45

























        answered Feb 1 at 19:35









        Jonathan AllanJonathan Allan

        52k535170




        52k535170












        • $begingroup$
          This appears to give the wrong answers if k or n are less than 2 (although I am not familiar enough with Jelly to know for sure).
          $endgroup$
          – PyRulez
          Feb 1 at 20:11












        • $begingroup$
          Fixed it to agree with given examples for a cost of 1 byte, are there any remaining issues?
          $endgroup$
          – Jonathan Allan
          Feb 1 at 20:48






        • 1




          $begingroup$
          I do not see any. The problems seemed related to empty and singleton lists, but those issues are now fixed.
          $endgroup$
          – PyRulez
          Feb 1 at 21:00


















        • $begingroup$
          This appears to give the wrong answers if k or n are less than 2 (although I am not familiar enough with Jelly to know for sure).
          $endgroup$
          – PyRulez
          Feb 1 at 20:11












        • $begingroup$
          Fixed it to agree with given examples for a cost of 1 byte, are there any remaining issues?
          $endgroup$
          – Jonathan Allan
          Feb 1 at 20:48






        • 1




          $begingroup$
          I do not see any. The problems seemed related to empty and singleton lists, but those issues are now fixed.
          $endgroup$
          – PyRulez
          Feb 1 at 21:00
















        $begingroup$
        This appears to give the wrong answers if k or n are less than 2 (although I am not familiar enough with Jelly to know for sure).
        $endgroup$
        – PyRulez
        Feb 1 at 20:11






        $begingroup$
        This appears to give the wrong answers if k or n are less than 2 (although I am not familiar enough with Jelly to know for sure).
        $endgroup$
        – PyRulez
        Feb 1 at 20:11














        $begingroup$
        Fixed it to agree with given examples for a cost of 1 byte, are there any remaining issues?
        $endgroup$
        – Jonathan Allan
        Feb 1 at 20:48




        $begingroup$
        Fixed it to agree with given examples for a cost of 1 byte, are there any remaining issues?
        $endgroup$
        – Jonathan Allan
        Feb 1 at 20:48




        1




        1




        $begingroup$
        I do not see any. The problems seemed related to empty and singleton lists, but those issues are now fixed.
        $endgroup$
        – PyRulez
        Feb 1 at 21:00




        $begingroup$
        I do not see any. The problems seemed related to empty and singleton lists, but those issues are now fixed.
        $endgroup$
        – PyRulez
        Feb 1 at 21:00











        1












        $begingroup$

        JavaScript (ES7),  123 116 115  113 bytes



        Takes input as (k)(n). Each necklace is represented as an array of integers.





        k=>g=(n,x=k**n,o=)=>x--?g(n,x,o.some(a=>~(a+[,a]).search(b),b=[...Array(n)].map(_=>x/k**--n%k|0))?o:[b,...o]):o


        Try it online!






        share|improve this answer











        $endgroup$


















          1












          $begingroup$

          JavaScript (ES7),  123 116 115  113 bytes



          Takes input as (k)(n). Each necklace is represented as an array of integers.





          k=>g=(n,x=k**n,o=)=>x--?g(n,x,o.some(a=>~(a+[,a]).search(b),b=[...Array(n)].map(_=>x/k**--n%k|0))?o:[b,...o]):o


          Try it online!






          share|improve this answer











          $endgroup$
















            1












            1








            1





            $begingroup$

            JavaScript (ES7),  123 116 115  113 bytes



            Takes input as (k)(n). Each necklace is represented as an array of integers.





            k=>g=(n,x=k**n,o=)=>x--?g(n,x,o.some(a=>~(a+[,a]).search(b),b=[...Array(n)].map(_=>x/k**--n%k|0))?o:[b,...o]):o


            Try it online!






            share|improve this answer











            $endgroup$



            JavaScript (ES7),  123 116 115  113 bytes



            Takes input as (k)(n). Each necklace is represented as an array of integers.





            k=>g=(n,x=k**n,o=)=>x--?g(n,x,o.some(a=>~(a+[,a]).search(b),b=[...Array(n)].map(_=>x/k**--n%k|0))?o:[b,...o]):o


            Try it online!







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 1 at 22:39

























            answered Feb 1 at 19:58









            ArnauldArnauld

            75.8k693319




            75.8k693319























                1












                $begingroup$


                Python 2, 105 bytes





                lambda k,n:{min(i[j:]+i[:j]for j in range(n or 1))for i in product(*[range(k)]*n)}
                from itertools import*


                Try it online!



                Returns a set of tuples.






                share|improve this answer









                $endgroup$


















                  1












                  $begingroup$


                  Python 2, 105 bytes





                  lambda k,n:{min(i[j:]+i[:j]for j in range(n or 1))for i in product(*[range(k)]*n)}
                  from itertools import*


                  Try it online!



                  Returns a set of tuples.






                  share|improve this answer









                  $endgroup$
















                    1












                    1








                    1





                    $begingroup$


                    Python 2, 105 bytes





                    lambda k,n:{min(i[j:]+i[:j]for j in range(n or 1))for i in product(*[range(k)]*n)}
                    from itertools import*


                    Try it online!



                    Returns a set of tuples.






                    share|improve this answer









                    $endgroup$




                    Python 2, 105 bytes





                    lambda k,n:{min(i[j:]+i[:j]for j in range(n or 1))for i in product(*[range(k)]*n)}
                    from itertools import*


                    Try it online!



                    Returns a set of tuples.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Feb 1 at 23:50









                    Erik the OutgolferErik the Outgolfer

                    31.8k429103




                    31.8k429103























                        0












                        $begingroup$


                        Japt, 27 bytes



                        ?UÆVoÃrï ®c £ZéYÃn gÃâ:[]


                        Try it online!



                        Outputs with numbers starting from 0 for the values. Inputs in reversed order (n then k). The r throws a fit for n = 0 so I had to add an significant special-case.



                        Explanation:



                        ?                     :[]    #Special case: return [] if n is 0
                        UÆ Ã #Get n rows of:
                        Vo # The numbers [0..k-1]
                        rï #Get all possible lists containing one number from each row...
                        ® Ã #For each of those lists:
                        c # Fix the formatting
                        £ZéYÃ # Get every possible rotation
                        n g # Choose the "minimum" one
                        â #Remove duplicate lists





                        share|improve this answer











                        $endgroup$













                        • $begingroup$
                          On input (3,4), it seems to be missing a necklace corresponding to [3,2,1].
                          $endgroup$
                          – PyRulez
                          Feb 1 at 23:51










                        • $begingroup$
                          @PyRulez Fixed, I think
                          $endgroup$
                          – Kamil Drakari
                          Feb 2 at 0:59
















                        0












                        $begingroup$


                        Japt, 27 bytes



                        ?UÆVoÃrï ®c £ZéYÃn gÃâ:[]


                        Try it online!



                        Outputs with numbers starting from 0 for the values. Inputs in reversed order (n then k). The r throws a fit for n = 0 so I had to add an significant special-case.



                        Explanation:



                        ?                     :[]    #Special case: return [] if n is 0
                        UÆ Ã #Get n rows of:
                        Vo # The numbers [0..k-1]
                        rï #Get all possible lists containing one number from each row...
                        ® Ã #For each of those lists:
                        c # Fix the formatting
                        £ZéYÃ # Get every possible rotation
                        n g # Choose the "minimum" one
                        â #Remove duplicate lists





                        share|improve this answer











                        $endgroup$













                        • $begingroup$
                          On input (3,4), it seems to be missing a necklace corresponding to [3,2,1].
                          $endgroup$
                          – PyRulez
                          Feb 1 at 23:51










                        • $begingroup$
                          @PyRulez Fixed, I think
                          $endgroup$
                          – Kamil Drakari
                          Feb 2 at 0:59














                        0












                        0








                        0





                        $begingroup$


                        Japt, 27 bytes



                        ?UÆVoÃrï ®c £ZéYÃn gÃâ:[]


                        Try it online!



                        Outputs with numbers starting from 0 for the values. Inputs in reversed order (n then k). The r throws a fit for n = 0 so I had to add an significant special-case.



                        Explanation:



                        ?                     :[]    #Special case: return [] if n is 0
                        UÆ Ã #Get n rows of:
                        Vo # The numbers [0..k-1]
                        rï #Get all possible lists containing one number from each row...
                        ® Ã #For each of those lists:
                        c # Fix the formatting
                        £ZéYÃ # Get every possible rotation
                        n g # Choose the "minimum" one
                        â #Remove duplicate lists





                        share|improve this answer











                        $endgroup$




                        Japt, 27 bytes



                        ?UÆVoÃrï ®c £ZéYÃn gÃâ:[]


                        Try it online!



                        Outputs with numbers starting from 0 for the values. Inputs in reversed order (n then k). The r throws a fit for n = 0 so I had to add an significant special-case.



                        Explanation:



                        ?                     :[]    #Special case: return [] if n is 0
                        UÆ Ã #Get n rows of:
                        Vo # The numbers [0..k-1]
                        rï #Get all possible lists containing one number from each row...
                        ® Ã #For each of those lists:
                        c # Fix the formatting
                        £ZéYÃ # Get every possible rotation
                        n g # Choose the "minimum" one
                        â #Remove duplicate lists






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Feb 2 at 0:59

























                        answered Feb 1 at 21:41









                        Kamil DrakariKamil Drakari

                        3,271416




                        3,271416












                        • $begingroup$
                          On input (3,4), it seems to be missing a necklace corresponding to [3,2,1].
                          $endgroup$
                          – PyRulez
                          Feb 1 at 23:51










                        • $begingroup$
                          @PyRulez Fixed, I think
                          $endgroup$
                          – Kamil Drakari
                          Feb 2 at 0:59


















                        • $begingroup$
                          On input (3,4), it seems to be missing a necklace corresponding to [3,2,1].
                          $endgroup$
                          – PyRulez
                          Feb 1 at 23:51










                        • $begingroup$
                          @PyRulez Fixed, I think
                          $endgroup$
                          – Kamil Drakari
                          Feb 2 at 0:59
















                        $begingroup$
                        On input (3,4), it seems to be missing a necklace corresponding to [3,2,1].
                        $endgroup$
                        – PyRulez
                        Feb 1 at 23:51




                        $begingroup$
                        On input (3,4), it seems to be missing a necklace corresponding to [3,2,1].
                        $endgroup$
                        – PyRulez
                        Feb 1 at 23:51












                        $begingroup$
                        @PyRulez Fixed, I think
                        $endgroup$
                        – Kamil Drakari
                        Feb 2 at 0:59




                        $begingroup$
                        @PyRulez Fixed, I think
                        $endgroup$
                        – Kamil Drakari
                        Feb 2 at 0:59











                        0












                        $begingroup$


                        Charcoal, 37 bytes



                        Nθ≔Xθ…⁰⊕Nη≔⊟ηζ¿ηIEΦζ⁼κ⌊﹪÷×ι⊕ζηζ﹪÷ιηθ¶


                        Try it online! Link is to verbose version of code. Explanation:



                        Nθ≔Xθ…⁰⊕Nη≔⊟ηζ


                        Input k and n and calculate the powers of k from 1 to n, but then keep kⁿ separately.



                        ¿η...¶


                        If n is zero then just print a newline. (I could special-case 0 in the algorithm which would only cost two bytes but the resulting array only contains an empty array and Charcoal optimises that away on output, so you can't tell that the empty array is there.)



                        Φζ⁼κ⌊﹪÷×ι⊕ζηζ


                        For all the numbers from 0 to kⁿ, multiply them all by kⁿ+1, then divide by the above powers of k, then take the modulus of all of them by kⁿ. Keep only those numbers which equal the minimum of the resulting set.



                        IE...﹪÷ιηθ


                        Convert the numbers to reversed base k padded to length n for output using Charcoal's default output which is each element on its own line and each necklace double-spaced from the next necklace.






                        share|improve this answer









                        $endgroup$


















                          0












                          $begingroup$


                          Charcoal, 37 bytes



                          Nθ≔Xθ…⁰⊕Nη≔⊟ηζ¿ηIEΦζ⁼κ⌊﹪÷×ι⊕ζηζ﹪÷ιηθ¶


                          Try it online! Link is to verbose version of code. Explanation:



                          Nθ≔Xθ…⁰⊕Nη≔⊟ηζ


                          Input k and n and calculate the powers of k from 1 to n, but then keep kⁿ separately.



                          ¿η...¶


                          If n is zero then just print a newline. (I could special-case 0 in the algorithm which would only cost two bytes but the resulting array only contains an empty array and Charcoal optimises that away on output, so you can't tell that the empty array is there.)



                          Φζ⁼κ⌊﹪÷×ι⊕ζηζ


                          For all the numbers from 0 to kⁿ, multiply them all by kⁿ+1, then divide by the above powers of k, then take the modulus of all of them by kⁿ. Keep only those numbers which equal the minimum of the resulting set.



                          IE...﹪÷ιηθ


                          Convert the numbers to reversed base k padded to length n for output using Charcoal's default output which is each element on its own line and each necklace double-spaced from the next necklace.






                          share|improve this answer









                          $endgroup$
















                            0












                            0








                            0





                            $begingroup$


                            Charcoal, 37 bytes



                            Nθ≔Xθ…⁰⊕Nη≔⊟ηζ¿ηIEΦζ⁼κ⌊﹪÷×ι⊕ζηζ﹪÷ιηθ¶


                            Try it online! Link is to verbose version of code. Explanation:



                            Nθ≔Xθ…⁰⊕Nη≔⊟ηζ


                            Input k and n and calculate the powers of k from 1 to n, but then keep kⁿ separately.



                            ¿η...¶


                            If n is zero then just print a newline. (I could special-case 0 in the algorithm which would only cost two bytes but the resulting array only contains an empty array and Charcoal optimises that away on output, so you can't tell that the empty array is there.)



                            Φζ⁼κ⌊﹪÷×ι⊕ζηζ


                            For all the numbers from 0 to kⁿ, multiply them all by kⁿ+1, then divide by the above powers of k, then take the modulus of all of them by kⁿ. Keep only those numbers which equal the minimum of the resulting set.



                            IE...﹪÷ιηθ


                            Convert the numbers to reversed base k padded to length n for output using Charcoal's default output which is each element on its own line and each necklace double-spaced from the next necklace.






                            share|improve this answer









                            $endgroup$




                            Charcoal, 37 bytes



                            Nθ≔Xθ…⁰⊕Nη≔⊟ηζ¿ηIEΦζ⁼κ⌊﹪÷×ι⊕ζηζ﹪÷ιηθ¶


                            Try it online! Link is to verbose version of code. Explanation:



                            Nθ≔Xθ…⁰⊕Nη≔⊟ηζ


                            Input k and n and calculate the powers of k from 1 to n, but then keep kⁿ separately.



                            ¿η...¶


                            If n is zero then just print a newline. (I could special-case 0 in the algorithm which would only cost two bytes but the resulting array only contains an empty array and Charcoal optimises that away on output, so you can't tell that the empty array is there.)



                            Φζ⁼κ⌊﹪÷×ι⊕ζηζ


                            For all the numbers from 0 to kⁿ, multiply them all by kⁿ+1, then divide by the above powers of k, then take the modulus of all of them by kⁿ. Keep only those numbers which equal the minimum of the resulting set.



                            IE...﹪÷ιηθ


                            Convert the numbers to reversed base k padded to length n for output using Charcoal's default output which is each element on its own line and each necklace double-spaced from the next necklace.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Feb 2 at 12:50









                            NeilNeil

                            80.5k744178




                            80.5k744178






























                                draft saved

                                draft discarded




















































                                If this is an answer to a challenge…




                                • …Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.


                                • …Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
                                  Explanations of your answer make it more interesting to read and are very much encouraged.


                                • …Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.



                                More generally…




                                • …Please make sure to answer the question and provide sufficient detail.


                                • …Avoid asking for help, clarification or responding to other answers (use comments instead).





                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f179386%2fgenerate-the-k-ary-necklaces-of-length-n%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                How to change which sound is reproduced for terminal bell?

                                Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                                Can I use Tabulator js library in my java Spring + Thymeleaf project?