# Counting Primitive Bit Strings in Python with SymPy

# Counting Primitive Bit Strings in Python with SymPy

Join the DZone community and get the full member experience.

Join For FreeA string of bits is called primitive if it is not the repetition of several copies of a smaller string of bits. For example, the 101101 is not primitive because it can be broken down into two copies of the string 101. In Python notation, you could produce 101101 by `"101"*2`

. The string 11001101, on the other hand, is primitive. (It contains substrings that are not primitive, but the string as a whole cannot be factored into multiple copies of a single string.)

For a given *n*, let’s count how many primitive bit strings there are of length *n*. Call this *f*(*n*). There are 2^{n} bit strings of length *n*, and *f*(*n*) of these are primitive. For example, there are *f*(12) primitive bit strings of length 12. The strings that are not primitive are made of copies of primitive strings: two copies of a primitive string of length 6, three copies of a primitive string of length 4, etc. This says

and in general

Here the sum is over all positive integers *d* that divide *n*.

Unfortunately this formula is backward. It gives is a formula for something well known, 2^{n}, as a sum of things we’re trying to calculate. The Möbius inversion formula is just what we need to turn this formula around so that the new thing is on the left and sums of old things are on the right. It tells us that

where μ is the Möbius function.

We could compute *f*(*n*) with Python as follows:

from sympy.ntheory import mobius, divisors def num_primitive(n): return sum( [mobius(n/d)*2**d for d in divisors(n)] )

The latest version of SymPy, version 0.7.6, comes with a function `mobius`

for computing the Möbius function. If you’re using an earlier version of SymPy, you can roll your own `mobius`

function:

from sympy.ntheory import factorint def mobius(n): exponents = factorint(n).values() lenexp = len(exponents) m = 0 if lenexp == 0 else max(exponents) return 0 if m > 1 else (-1)**lenexp

The version of `mobius`

that comes with SymPy 0.7.6 may be more efficient. It could, for example, stop the factorization process early if it discovers a square factor.

Published at DZone with permission of John Cook , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

## {{ parent.title || parent.header.title}}

{{ parent.tldr }}

## {{ parent.linkDescription }}

{{ parent.urlSource.name }}