Skip to content

Slightly Better Rotational Asymmetry Binary Generator #4

Description

@RulOfficial
"""Return the number of distinct binary necklaces of length n using
the FKM / Ruskey algorithm, without storing them, based on Lyndon words."""

def generate_necklaces_fkm(n: int):
    buffer = [0] * (n + 1)
    necklaces = []

    def generate_recursive(position: int, period: int) -> None:
        if position > n:
            if n % period == 0:
                necklace = "".join(str(bit) for bit in buffer[1 : period + 1]) * (n // period)
                necklaces.append(necklace)
            return

        buffer[position] = buffer[position - period]
        generate_recursive(position + 1, period)

        for bit_value in range(buffer[position - period] + 1, 2):
            buffer[position] = bit_value
            generate_recursive(position + 1, position)

    generate_recursive(1, 1)
    return necklaces


for necklace in generate_necklaces_fkm(13):
    print(necklace)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions