Skip to main content
added 457 characters in body
Source Link
Dominic van Essen
  • 36.4k
  • 2
  • 22
  • 60

Husk, 10 bytes

#ȯΠṁoẊ≠½Qπ

Try it online! or Try first elements of first few test cases

Input is arg1=k, arg2=n. Outputs number of square-free words of length n, using an alphabet with k symbols.

         π  # Cartesian arg2-th power of range [1..arg1] 
#ȯ          # how many truthy elements of the following function:
        Q   #   get all contiguous sublists
   ṁo       #   and for each of them:
       ½    #     split them in half
     Ẋ≠     #     and check if both halves are unequal
  Π         #   now take the product of all of that 
            #   (so product is zero if any sublist contained
            #   non-unequal halves, non-zero [truthy] otherwise)

Husk, 10 bytes

#ȯΠṁoẊ≠½Qπ

Try it online! or Try first elements of first few test cases

Input is arg1=k, arg2=n. Outputs number of square-free words of length n, using an alphabet with k symbols.

Husk, 10 bytes

#ȯΠṁoẊ≠½Qπ

Try it online! or Try first elements of first few test cases

Input is arg1=k, arg2=n. Outputs number of square-free words of length n, using an alphabet with k symbols.

         π  # Cartesian arg2-th power of range [1..arg1] 
#ȯ          # how many truthy elements of the following function:
        Q   #   get all contiguous sublists
   ṁo       #   and for each of them:
       ½    #     split them in half
     Ẋ≠     #     and check if both halves are unequal
  Π         #   now take the product of all of that 
            #   (so product is zero if any sublist contained
            #   non-unequal halves, non-zero [truthy] otherwise)
Source Link
Dominic van Essen
  • 36.4k
  • 2
  • 22
  • 60

Husk, 10 bytes

#ȯΠṁoẊ≠½Qπ

Try it online! or Try first elements of first few test cases

Input is arg1=k, arg2=n. Outputs number of square-free words of length n, using an alphabet with k symbols.