Project Euler Problem 239

A set of disks numbered 1 through 100 are placed in a line in random order.

Project Euler Problem 239

Solution

Answer: 0.001887854841

There are $100$ disks, of which the primes among $1,\dots,100$ are:

$$2,3,5,7,11,\dots,97$$

The number of primes $\le 100$ is

$$\pi(100)=25.$$

We want the probability that exactly $22$ prime-numbered disks are not in their natural positions.

Equivalently:

  • among the $25$ prime disks,
  • exactly $3$ are fixed,
  • the other $22$ are moved.

The non-prime disks are unrestricted.


Step 1 — Reformulating the counting problem

Let:

  • $P$ = set of prime positions, $|P|=25$,
  • $C$ = set of composite/non-prime positions, $|C|=75$.

We seek:

$$\Pr(\text{exactly 3 prime disks fixed})$$

over all $100!$ permutations.

Choose which $3$ prime disks stay fixed:

$$\binom{25}{3}.$$

Now the remaining $22$ prime disks must not be fixed.

No restrictions are imposed on the $75$ non-prime disks.

So after fixing those $3$ primes, we must count permutations of the remaining $97$ elements in which the specified $22$ prime disks avoid their own positions.

This is a standard inclusion–exclusion problem.


Step 2 — Inclusion–Exclusion

Let the remaining $22$ prime disks be distinguished.

For each such disk $i$, define event:

$$A_i = {\text{disk } i \text{ is fixed}}.$$

We want permutations avoiding all $A_i$.

Total permutations of remaining $97$ objects:

$$97!.$$

If we force a particular subset of $k$ of those $22$ disks to be fixed, then:

$$(97-k)!$$

permutations remain.

Thus by inclusion–exclusion:

$$N = \sum_{k=0}^{22} (-1)^k \binom{22}{k} (97-k)!.$$

Including the choice of the $3$ fixed primes:

$$#(\text{desired permutations}) = \binom{25}{3} \sum_{k=0}^{22} (-1)^k \binom{22}{k} (97-k)!.$$

Hence the probability is

$$\boxed{ \frac{ \binom{25}{3} \displaystyle\sum_{k=0}^{22} (-1)^k \binom{22}{k} (97-k)! }{ 100! } }.$$


Step 3 — Python implementation

from math import comb, factorial
from decimal import Decimal, getcontext

# use high precision
getcontext().prec = 50

total = factorial(100)

# inclusion-exclusion sum
s = 0
for k in range(23):
    s += (-1)**k * comb(22, k) * factorial(97 - k)

count = comb(25, 3) * s

prob = Decimal(count) / Decimal(total)

print(prob)
print(round(prob, 12))

Step 4 — Code walkthrough

Imports

from math import comb, factorial
  • comb(n,r) computes binomial coefficients.
  • factorial(n) computes factorials exactly.
from decimal import Decimal, getcontext

Used for high-precision decimal arithmetic.


Precision

getcontext().prec = 50

Ensures enough precision before rounding to 12 decimal places.


Total permutations

total = factorial(100)

There are $100!$ equally likely permutations.


Inclusion–exclusion

s = 0
for k in range(23):

We sum over all possible numbers $k$ of forbidden prime disks accidentally fixed.

s += (-1)**k * comb(22, k) * factorial(97 - k)
  • choose which $k$ disks are fixed,
  • fix them,
  • permute the remaining $97-k$ elements.

Alternating signs implement inclusion–exclusion.


Choose the 3 fixed primes

count = comb(25, 3) * s

Exactly $3$ prime disks are fixed.


Probability

prob = Decimal(count) / Decimal(total)

Divide by total permutations.


Step 5 — Numerical evaluation

Evaluating the expression gives:

$$0.001887854841\dots$$

Rounded to 12 digits after the decimal point:

$$\boxed{0.001887854841}$$

This magnitude is sensible:

  • expected fixed primes among 25 is roughly $25 \cdot \frac1{100}=0.25$,
  • having exactly 3 fixed primes is rare but not astronomically so,
  • probability around $10^{-3}$ is plausible.

Answer: 0.001887854841