[Python] How to find Lyndon words among necklaces? (Or how to find the periodic necklaces?)

Discussão em 'Python' iniciado por Stack, Agosto 10, 2025.

  1. Stack

    Stack Membro Participativo

    There are A003239(n) binary necklaces of length 2n.
    A022553(n) among them are primitive, i.e. not periodic. They correspond to Lyndon words.
    For n=6 there are 80 necklaces. 75 of them are primitive, leaving 5 that must be periodic.

    It should be trivial to show examples with Python, but something is wrong with my code.
    For n=6 it finds only 4 instead of 5 periodic necklaces.
    (For n=7 it does not find the single periodic necklace. For n=8 all 10 are found.)

    from itertools import combinations
    from math import log10, ceil


    def necklace_representative(necklace):

    def rotate(key):
    return necklace[key:] + necklace[:key]

    rotations = [rotate(i) for i in range(len(necklace))]

    return min(rotations)


    n = 6
    length = 2 * n
    necklaces = set()

    for places in combinations(range(length), n):
    word = [int(i in places) for i in range(length)]
    necklaces.add(tuple(necklace_representative(word)))

    decimal_length = ceil(log10(len(necklaces) + 1))

    for i, word in enumerate(sorted(necklaces)):

    print(
    str(i+1).zfill(decimal_length),
    ''.join([str(_) for _ in word]),
    'periodic' if word[0:n] == word[n:length] else ''
    )


    01 000000111111
    02 000001011111
    03 000001101111
    04 000001110111
    05 000001111011
    06 000001111101
    07 000010011111
    08 000010101111
    09 000010110111
    10 000010111011
    11 000010111101
    12 000011001111
    13 000011010111
    14 000011011011
    15 000011011101
    16 000011100111
    17 000011101011
    18 000011101101
    19 000011110011
    20 000011110101
    21 000011111001
    22 000100011111
    23 000100101111
    24 000100110111
    25 000100111011
    26 000100111101
    27 000101001111
    28 000101010111
    29 000101011011
    30 000101011101
    31 000101100111
    32 000101101011
    33 000101101101
    34 000101110011
    35 000101110101
    36 000101111001
    37 000110001111
    38 000110010111
    39 000110011011
    40 000110011101
    41 000110100111
    42 000110101011
    43 000110101101
    44 000110110011
    45 000110110101
    46 000110111001
    47 000111000111 periodic
    48 000111001011
    49 000111001101
    50 000111010011
    51 000111010101
    52 000111011001
    53 000111100101
    54 000111101001
    55 001001001111
    56 001001010111
    57 001001011011
    58 001001011101
    59 001001100111
    60 001001101011
    61 001001101101
    62 001001110011
    63 001001110101
    64 001010010111
    65 001010011011
    66 001010011101
    67 001010100111
    68 001010101011
    69 001010101101
    70 001010110011
    71 001010110101
    72 001011001011 periodic
    73 001011001101
    74 001011010011
    75 001011010101
    76 001100110011
    77 001100110101
    78 001101001101 periodic
    79 001101010101
    80 010101010101 periodic

    Continue reading...

Compartilhe esta Página