Project Euler Problem 281

You are given a pizza (perfect circle) that has been cut into m cdot n equal pieces and you want to have exactly one top

Project Euler Problem 281

Solution

Answer: 1485776387445623

Let $N = mn$.

We must count circular arrangements of $N$ pizza slices using $m$ toppings, each appearing exactly $n$ times.

Rotations are identified, reflections are distinct.

We define:

f(m,n)=\text{# circular arrangements of } mn \text{ slices with } m \text{ toppings each used } n \text{ times}.

We seek

$$\sum_{f(m,n)\le 10^{15}} f(m,n).$$


1. Mathematical analysis

This is a classic application of Burnside’s Lemma on cyclic rotations.

Let the cyclic group $C_{mn}$ act on the arrangements by rotation.


Step 1: Burnside's Lemma

Burnside gives:

$$f(m,n)=\frac1{mn}\sum_{r=0}^{mn-1} \mathrm{Fix}(r),$$

where $\mathrm{Fix}(r)$ is the number of arrangements unchanged by rotation by $r$ slices.


Step 2: Structure of a rotation

For a rotation by $r$, let

$$g=\gcd(r,mn).$$

Then the rotation decomposes the $mn$ positions into:

  • $g$ cycles,
  • each of length

$$L=\frac{mn}{g}.$$

For an arrangement to remain fixed, every position in a cycle must carry the same topping.

Thus each topping count $n$ must be divisible by the cycle length $L$:

$$L \mid n.$$

So only divisors $L$ of $n$ contribute.


Step 3: Counting fixed arrangements

Suppose $L\mid n$, and define

$$d=\frac{n}{L}.$$

Then each topping occupies exactly $d$ cycles.

Since there are

$$g=\frac{mn}{L}=md$$

cycles total, the number of ways to assign cycles to toppings is

$$\frac{(md)!}{(d!)^m}.$$


Step 4: How many rotations have cycle length $L$?

A rotation has cycle length $L$ exactly when

$$\frac{mn}{\gcd(r,mn)}=L.$$

The number of such rotations is Euler’s totient:

$$\varphi(L).$$

Hence Burnside becomes

$$f(m,n) = \frac1{mn} \sum_{L\mid n} \varphi(L), \frac{(mn/L)!}{((n/L)!)^m}.$$

Substituting $d=n/L$:

$f(m,n)=\frac{1}{mn}\sum_{d\mid n}\varphi!\left(\frac{n}{d}\right)\frac{(md)!}{(d!)^m}$

This is the formula we compute.


2. Bounding the search

The values grow extremely quickly.

For $n=1$,

$$f(m,1)=(m-1)!.$$

Since

$$18! / 18 = 17! = 355687428096000 \le 10^{15},$$

but

$$19! / 19 = 18! > 10^{15},$$

we only need $m\le 18$.

Similarly, for each fixed $m$, $f(m,n)$ grows rapidly with $n$, so only a tiny finite set of pairs survives.

In fact there are only 74 valid pairs $(m,n)$.


3. Python implementation

from math import factorial
from sympy import divisors, totient

LIMIT = 10**15

def f(m, n):
    """
    Compute:
        f(m,n) = number of circular arrangements
    using Burnside's lemma.
    """
    total = 0

    for d in divisors(n):
        total += (
            totient(n // d)
            * factorial(m * d)
            // (factorial(d) ** m)
        )

    return total // (m * n)

answer = 0

m = 2

while True:

    # Minimal value for this m occurs at n = 1
    if f(m, 1) > LIMIT:
        break

    n = 1

    while True:
        value = f(m, n)

        if value > LIMIT:
            break

        answer += value
        n += 1

    m += 1

print(answer)

4. Code walkthrough

Imports

from math import factorial
from sympy import divisors, totient

We need:

  • factorials,
  • divisor enumeration,
  • Euler’s totient function.

Function f(m,n)

for d in divisors(n):

Iterates over all divisors $d\mid n$.

Each term contributes

totient(n // d)
* factorial(m * d)
// (factorial(d) ** m)

which is exactly

$$\varphi!\left(\frac nd\right)\frac{(md)!}{(d!)^m}.$$

Finally divide by $mn$:

return total // (m * n)

Outer search over $m$

if f(m, 1) > LIMIT:
    break

Since $f(m,n)$ increases rapidly with $n$, once even $n=1$ exceeds the limit, all larger $n$ do too.


Inner search over $n$

if value > LIMIT:
    break

Once values exceed the limit for fixed $m$, we stop.


5. Verification on small examples

The program gives:

$$f(2,1)=1$$

$$f(2,2)=2$$

$$f(3,1)=2$$

$$f(3,2)=16$$

matching the statement exactly.


6. Final verification

The computation enumerates all valid pairs $(m,n)$ with

$$f(m,n)\le 10^{15}.$$

There are only finitely many because factorial growth dominates.

The resulting total is:

$$1485776387445623.$$

Answer: 1485776387445623