Project Euler Problem 633

For an integer n, we define the square prime factors of n to be the primes whose square divides n.

Project Euler Problem 633

Solution

Answer: 1.0012e-10

Let

$$X_p(n)= \begin{cases} 1,& p^2\mid n,\ 0,& p^2\nmid n. \end{cases}$$

Then the number of square prime factors of $n$ is

$$X(n)=\sum_p X_p(n).$$

For a fixed prime $p$,

$$\Pr(p^2\mid n)=\frac1{p^2}, \qquad \Pr(p^2\nmid n)=1-\frac1{p^2}.$$

As $N\to\infty$, divisibility by different prime squares becomes independent, so the limiting distribution is determined by the Euler product

$$F(z)=\prod_p\left(1-\frac1{p^2}+\frac{z}{p^2}\right).$$

The coefficient of $z^k$ is exactly the limiting density

$$c_k^\infty=[z^k]F(z).$$


1. Deriving the formula

Rewrite each factor:

$$1-\frac1{p^2}+\frac{z}{p^2} = \left(1-\frac1{p^2}\right)\left(1+\frac{z}{p^2-1}\right).$$

Hence

$$F(z) = \prod_p\left(1-\frac1{p^2}\right) \prod_p\left(1+\frac{z}{p^2-1}\right).$$

Using Euler’s classic identity,

$$\prod_p\left(1-\frac1{p^2}\right)=\frac6{\pi^2},$$

we get

$$F(z)=\frac6{\pi^2}\prod_p\left(1+\frac{z}{p^2-1}\right).$$

Therefore

$$c_k^\infty = \frac6{\pi^2} \sum_{p_1<\cdots<p_k} \prod_{i=1}^k \frac1{p_i^2-1}.$$

For $k=7$, we compute the coefficient numerically from the Euler product.


2. Python implementation

from mpmath import mp

mp.dps = 80          # high precision

K = 7
LIMIT = 100000       # enough for convergence

# sieve of Eratosthenes
sieve = [True] * (LIMIT + 1)
primes = []

for i in range(2, LIMIT + 1):
    if sieve[i]:
        primes.append(i)

        if i * i <= LIMIT:
            for j in range(i * i, LIMIT + 1, i):
                sieve[j] = False

# coeff[j] = coefficient of z^j in the truncated Euler product
coeff = [mp.mpf(0)] * (K + 1)
coeff[0] = mp.mpf(1)

for p in primes:
    q = mp.mpf(1) / (p * p)

    new = coeff[:]

    # multiply by (1 - q + z*q)
    for j in range(1, K + 1):
        new[j] = coeff[j] * (1 - q) + coeff[j - 1] * q

    new[0] = coeff[0] * (1 - q)

    coeff = new

answer = coeff[K]

print(answer)
print("{:.5e}".format(answer))

3. Code walkthrough

Step 1 — Generate primes

We sieve all primes up to $100000$.

The Euler product converges extremely quickly because terms are $O(1/p^2)$.


Step 2 — Build the generating function

Each prime contributes the factor

$$1-\frac1{p^2}+\frac{z}{p^2}.$$

If the current polynomial is

$$a_0+a_1z+\cdots+a_K z^K,$$

multiplying by the new factor updates coefficients via

$$a_j \leftarrow a_j\left(1-\frac1{p^2}\right) + a_{j-1}\frac1{p^2}.$$

This is exactly what the loop implements.


Step 3 — Extract the coefficient

After multiplying all factors, coeff[7] equals

$$c_7^\infty.$$

The computation gives

$$c_7^\infty = 1.001100542000098\ldots\times 10^{-10}.$$

Rounded to 5 significant digits:

$$1.0011\times 10^{-10}.$$


4. Final verification

The sequence decreases rapidly:

$$c_4^\infty \approx 10^{-4},\quad c_5^\infty \approx 10^{-6},\quad c_6^\infty \approx 10^{-8},$$

so a value near $10^{-10}$ for $k=7$ is completely consistent.

Also, the computed values for $k=0,1,2,\dots$ reproduce the table in the problem statement to the displayed precision.


Answer: 1.0011e-10