# Primitive binary necklaces

The problem/solution of counting the number of (primitive) necklaces is very well known.

But what about results giving sufficient conditions for a given necklace be primitive? For example, in the binary case, a necklace of length $N$ (00..00100..01) will be primitive whenever the number $M$ of zeros between the two 1's is such that $\gcd(N,M)=1$.

Any idea/references for additional results of this type?

• Are cyclically permuted necklaces considered the same necklace? Mar 10, 2013 at 11:10
• @AlexanderGruber binary necklace is an equivalence class of binary strings under rotation. If the strings have length n, then the size of the equivalence class is at most n (it's actually a divisor of n). We call the necklace primitive if size of the equivalence class is is large as possible; that is, the size is n. Mar 10, 2013 at 12:40

The number of primitive strings of length $n$, call it $p_n$, is easy to get. The total number of length $n$ strings made of $s$ symbols is just $s^n$. All strings are made of repeats of some primitive string (in case it is primitive, it is one repeat). So: $$s^n = \sum_{d \mid n} p_d$$ and Möbius inversion gives: $$p_n = \sum_{d \mid n} \mu(n/d) s^d$$ Now each primitive necklace (i.e., cycle) can be cut at $n$ places to get different primitive strings, so the number of primitive necklaces is just: $$c_n = \frac{1}{n} \sum_{d \mid n} \mu(n/d) s^d$$

• To @vonbrand, I was able to finish a computation that you started, it is at this MSE link. Jul 1, 2014 at 2:46

I would like to add some linked material from the OEIS to this thread in order to facilitate further exploration of this problem. (The amount of material at the OEIS indicates that this problem has interested many combinatorics enthusiasts.) We show how to count primitive necklaces of at most $N$ colors. Recall that the Polya Enumeration Theorem tells us that the total number of necklaces (rotational symmetry only) on at most $N$ colors is given by the substituted cycle index of the cyclic group $$Z(C_n) = \frac{1}{n}\sum_{d|n} \varphi(d) a_d^{n/d}$$ which is, supposing there are $N$ colors, $$q_n = Z(C_n)(c_1+c_2+\cdots+c_N)_{c_1=c_2=\cdots=c_N=1} \\= \left. \frac{1}{n}\sum_{d|n} \varphi(d) (c_1^d+c_2^d+\cdots+c_N^d)^{n/d} \right|_{c_1=c_2=\cdots=c_N=1} = \frac{1}{n}\sum_{d|n} \varphi(d) N^{n/d}.$$ Let the number of primitive necklaces be $p_n$, then it is perfectly straightforward to show that $$\sum_{d|n} p_d = q_n.$$ Apply Moebius inversion to this to obtain $$p_n = \sum_{d|n} \frac{\mu(n/d)}{d} \left(\sum_{f|d} \varphi(f) N^{d/f}\right).$$

Here is the Maple code to compute this function.

with(numtheory);

p := proc(n, N)
local d, f, res;
res := 0;
for d in numtheory:-divisors(n) do for f in numtheory:-divisors(d) do
res := res + numtheory:-mobius(n/d)*numtheory:-phi(f)*N^(d/f)/d
end do
end do;
res
end proc


This gives for two colors ($N=2$) the sequence $$2, 1, 2, 3, 6, 9, 18, 30, 56, 99, 186, 335, 630, 1161, 2182,\ldots$$ which is OEIS A001037.

For three colors ($N=3$) we get the sequence $$3, 3, 8, 18, 48, 116, 312, 810, 2184, 5880, 16104, 44220, 122640, 341484, 956576,\ldots$$ which is OEIS A027376.

Finally for four colors ($N=4$) we get the sequence $$4, 6, 20, 60, 204, 670, 2340, 8160, 29120, 104754, 381300, 1397740, 5162220,\ldots$$ which is OEIS A027377.

Addendum. According to the OEIS, the above formula for $p_n$ simplifies to $$p_n = \frac{1}{n}\sum_{d|n} \mu(d) N^{n/d}.$$

The simplification of the above goes like this.

Start with $$p_n = \sum_{d|n} \frac{\mu(n/d)}{d} \left(\sum_{f|d} \varphi(f) N^{d/f}\right).$$

Re-index setting $d/f=q$ so that $d=fq$ to obtain $$p_n = \sum_{q|n} N^q \sum_{f|n/q} \frac{\mu(n/f/q)}{fq} \varphi(f).$$

This is $$p_n = \frac{1}{n} \sum_{q|n} N^q \sum_{f|n/q} \frac{\mu(n/f/q)}{fq/n} \varphi(f).$$ Putting $m= n/q$ the inner sum becomes $$\sum_{f|m} \mu(m/f) \times m/f \times \varphi(f).$$

This is a convolution of multiplicative functions so we only need to verify it for prime powers $m = p^v.$ This gives two terms namely for $f = p^v$ and $f = p^{v-1}$, which for $v\ge 2$ contribute $$1 \times 1 \times \varphi(p^v) - 1 \times p \times \varphi(p^{v-1}) = p^v - p^{v-1} - p (p^{v-1} - p^{v-2}) = 0.$$ For $v=1$ we get $$1 \times 1 \times \varphi(p) - 1 \times p \times \varphi(1) = p-1 - p = -1.$$ Finally for $m=1$ we get $1.$ This shows that (using the convolution being multiplicative) $$\sum_{f|m} \mu(m/f) \times m/f \times \varphi(f) = \mu(m)$$ and we finally have $$p_n = \frac{1}{n} \sum_{q|n} N^q \mu(n/q).$$

Here is another answer that presents an additional twist to the problem of counting primitive necklaces, namely Power Group Enumeration (as presented by Harary and Palmer and Fripertinger, in a different publication), with the group acting on the slots where the $N$ colors are placed being the cyclic group $C_n$ on $n$ elements and the group acting on the colors being the symmetric group on $N$ elements $S_N$.

This treats the problem of counting primitive necklaces where colors may be swapped without the resulting necklaces being considered different.

As in the other answer we have the relation $$\sum_{d|n} p_d = q_n,$$ but now $q_n$ is computed by Power Group Enumeration. No formula will be given but we will explain how to compute $q_n$ and present the relevant Maple code. The desired value for primitive necklaces then follows by $$p_n = \sum_{d|n} \mu(n/d) q_d.$$

We can compute the number $q_n$ of configurations by Burnside's lemma which says to average the number of assignments fixed by the elements of the power group, which has $N!\times |C_n|$ elements and $|C_n|=n$. But this number is easy to compute. Suppose we have a permutation $\alpha$ from $C_n$ and a permutation $\beta$ from $S_N.$ If we place the appropriate number of complete, directed and consecutive copies of a cycle from $\beta$ on a cycle from $\alpha$ then this assignment is fixed under the power group action, and this is possible iff the length of the cycle from $\beta$ divides the length of the cycle from $\alpha$ and there are as many assignments as the length of $\beta.$

Now the Burnside computation is best done with a CAS, here is the Maple code.

with(numtheory);

pet_cycleind_symm :=
proc(n)
option remember;

if n=0 then return 1; fi;

end;

pet_flatten_term :=
proc(varp)
local terml, d, cf, v;

terml := [];

cf := varp;
for v in indets(varp) do
d := degree(varp, v);
terml := [op(terml), seq(v, k=1..d)];
cf := cf/v^d;
od;

[cf, terml];
end;

pet_cycleind_cyclic :=
proc(n)
end;

q :=
proc(n, N)
option remember;
local idx_colors, res, a, b,
flat_a, flat_b, cyc_a, cyc_b, len_a, len_b, p, q;

if n=1 then
idx_colors := [a]
else
idx_colors := pet_cycleind_symm(N);
fi;

res := 0;

for a in pet_cycleind_cyclic(n) do
flat_a := pet_flatten_term(a);

for b in idx_colors do
flat_b := pet_flatten_term(b);

p := 1;
for cyc_a in flat_a do
len_a := op(1, cyc_a);
q := 0;

for cyc_b in flat_b do
len_b := op(1, cyc_b);

if len_a mod len_b = 0 then
q := q + len_b;
fi;
od;

p := p*q;
od;

res := res + p*flat_a*flat_b;
od;
od;

res;
end;

p := (n, N) -> add(mobius(n/d)*q(d,N), d in divisors(n));


This gives for two colors ($N=2$) the sequence $$1, 1, 1, 2, 3, 5, 9, 16, 28, 51, 93, 170,\ldots$$ which is OEIS A000048.

For three colors ($N=3$) we get the sequence $$1, 1, 2, 4, 8, 22, 52, 140, 366, 992, 2684, 7404,\ldots$$ which is OEIS A002075.

Finally for four colors ($N=4$) we get the sequence $$1, 1, 2, 5, 10, 35, 102, 360, 1232, 4427, 15934, 58465,\ldots$$ which is OEIS A056300.

Additional linkage. For $N=5$ we find a the sequence $$1, 1, 2, 5, 11, 38, 122, 496, 2005, 8707, 38364, 173562,\ldots$$ which is OEIS A056301.

For $N=6$ we finde the sequence $$1, 1, 2, 5, 11, 39, 125, 532, 2301, 11010, 54681, 284023,\ldots$$ which is OEIS A056302.

For $N=7$ we find the sequence $$1, 1, 2, 5, 11, 39, 126, 536, 2353, 11606, 60498, 336399,\ldots$$ which is not yet in the OEIS.

Addendum Sat Apr 21 2018. The algorithm just presented admits of a considerable improvement, namely that there is no need to flatten the permutations because we can compute the contribution from the cycles of a pair $(\alpha, \beta)$ by multiplying the number of coverings of a cycle type from $\alpha$ by the number of instances of the cycle type from $\beta$, raising the result to the power of the number of instances of the cycle type from $\alpha.$

This yields the following Maple code.

with(numtheory);

pet_cycleind_symm :=
proc(n)
option remember;

if n=0 then return 1; fi;

end;

pet_cycleind_cyclic :=
proc(n)
end;

q := proc(n, N)
option remember;
local idx_slots, idx_colors, res, term_a, term_b,
v_a, v_b, inst_a, inst_b, len_a, len_b, p, q;

if n = 1 then
idx_slots := [a];
else
idx_slots := pet_cycleind_cyclic(n);
fi;

if N = 1 then
idx_colors := [a];
else
idx_colors := pet_cycleind_symm(N);
fi;

res := 0;

for term_a in idx_slots do
for term_b in idx_colors do
p := 1;

for v_a in indets(term_a) do
len_a := op(1, v_a);
inst_a := degree(term_a, v_a);

q := 0;

for v_b in indets(term_b) do
len_b := op(1, v_b);
inst_b := degree(term_b, v_b);

if len_a mod len_b = 0 then
q := q + len_b*inst_b;
fi;
od;

p := p*q^inst_a;
od;

res := res +
lcoeff(term_a)*lcoeff(term_b)*p;
od;
od;

res;
end;

p := (n, N) -> add(mobius(n/d)*q(d,N), d in divisors(n));