All Euler problems
Project Euler

Euler Product Approximation

The Euler product for the Riemann zeta function is zeta(s) = prod_(p prime) (1 - p^(-s))^(-1). Let E(s, N) = prod_(p <= N) (1 - p^(-s))^(-1). Find floor(E(3, 10^6) x 10^15).

Source sync Apr 19, 2026
Problem #0951
Level Level 26
Solved By 395
Languages C++, Python
Answer 495568995495726
Length 337 words
number_theoryanalytic_mathbrute_force

Problem Statement

This archive keeps the full statement, math, and original media on the page.

Two players play a game using a deck of cards: red and black. Initially the deck is shuffled into one of the possible starting configurations. Play then proceeds, alternating turns, where a player follows two steps on each turn:

  1. Remove the top card from the deck, taking note of its colour.
  2. If there is a next card and it is the same colour as the previous card they toss a fair coin. If the coin lands on heads they remove that card as well; otherwise leave it on top of the deck.

The player who removes the final card from the deck wins the game.

Some starting configurations give an advantage to one of the players; while some starting configurations are fair, in which both players have exactly chance to win the game. For example, if there are four starting configurations which are fair: RRBB, BBRR, RBBR, BRRB. The remaining two, RBRB and BRBR, result in a guaranteed win for the second player.

Define to be the number of starting configurations which are fair. Therefore . You are also given .

Find .

Problem 951: Euler Product Approximation

Mathematical Foundation

Theorem 1 (Euler Product Formula). For Re(s)>1\operatorname{Re}(s) > 1,

ζ(s)=n=1ns=p prime11ps.\zeta(s) = \sum_{n=1}^{\infty} n^{-s} = \prod_{p \text{ prime}} \frac{1}{1 - p^{-s}}.

Proof. Each factor expands as a geometric series: (1ps)1=k=0pks(1 - p^{-s})^{-1} = \sum_{k=0}^{\infty} p^{-ks}, convergent for Re(s)>0\operatorname{Re}(s) > 0. For any finite set of primes PP,

pPk=0pks=n1pnpPns\prod_{p \in P} \sum_{k=0}^{\infty} p^{-ks} = \sum_{\substack{n \ge 1 \\ p \mid n \Rightarrow p \in P}} n^{-s}

by the Fundamental Theorem of Arithmetic: every positive integer factors uniquely into primes, so expanding the product yields exactly one term nsn^{-s} for each nn whose prime factors all lie in PP. As PP exhausts all primes, the right-hand side converges to ζ(s)\zeta(s) by absolute convergence of the Dirichlet series for Re(s)>1\operatorname{Re}(s) > 1. \square

Lemma 1 (Monotonicity). E(s,N)ζ(s)E(s, N) \le \zeta(s) for all NN, with E(s,N)ζ(s)E(s, N) \nearrow \zeta(s) as NN \to \infty.

Proof. Each factor (1ps)1>1(1 - p^{-s})^{-1} > 1 for s>0s > 0. The partial product E(s,N)E(s, N) accounts for all positive integers whose prime factors are at most NN, a subset of all positive integers. Including additional primes strictly increases the product. \square

Theorem 2 (Error Bound). For s=3s = 3 and N2N \ge 2,

ζ(3)E(3,N)=O ⁣(1N2lnN).\zeta(3) - E(3, N) = O\!\left(\frac{1}{N^2 \ln N}\right).

Proof. We have ζ(3)/E(3,N)=p>N(1p3)1\zeta(3)/E(3,N) = \prod_{p > N}(1-p^{-3})^{-1}. Taking logarithms:

lnζ(3)E(3,N)=p>Nk=11kp3k=p>Np3+O ⁣(p>Np6).\ln\frac{\zeta(3)}{E(3,N)} = \sum_{p > N} \sum_{k=1}^{\infty} \frac{1}{k p^{3k}} = \sum_{p > N} p^{-3} + O\!\left(\sum_{p > N} p^{-6}\right).

By partial summation with the Prime Number Theorem π(x)x/lnx\pi(x) \sim x/\ln x:

p>Np3=π(N)N3+3Nπ(t)t4dt3N1t3lntdt32N2lnN.\sum_{p > N} p^{-3} = -\frac{\pi(N)}{N^3} + 3\int_N^{\infty} \frac{\pi(t)}{t^4}\,dt \sim 3\int_N^{\infty} \frac{1}{t^3 \ln t}\,dt \sim \frac{3}{2N^2 \ln N}.

Since ln(1+ε)ε\ln(1 + \varepsilon) \sim \varepsilon for small ε\varepsilon, we obtain ζ(3)/E(3,N)1=O(1/(N2lnN))\zeta(3)/E(3,N) - 1 = O(1/(N^2 \ln N)), and therefore ζ(3)E(3,N)=ζ(3)O(1/(N2lnN))=O(1/(N2lnN))\zeta(3) - E(3,N) = \zeta(3) \cdot O(1/(N^2 \ln N)) = O(1/(N^2 \ln N)). \square

Lemma 2 (Numerical Precision). For N=106N = 10^6, the relative error (ζ(3)E(3,N))/ζ(3)(\zeta(3) - E(3,N))/\zeta(3) is approximately 3×10143 \times 10^{-14}. Standard IEEE 754 double-precision arithmetic (53-bit mantissa, \sim15.9 decimal digits) suffices for extracting E(3,106)×1015\lfloor E(3,10^6) \times 10^{15} \rfloor.

Proof. By Theorem 2, the relative error is 3/(210126ln10)1.1×1014\approx 3/(2 \cdot 10^{12} \cdot 6\ln 10) \approx 1.1 \times 10^{-14}. Working in log-space (accumulating L=pNln(1p3)L = \sum_{p \le N} -\ln(1-p^{-3})), each term has relative error εmach253\varepsilon_{\mathrm{mach}} \approx 2^{-53}. Over π(106)=78498\pi(10^6) = 78498 terms, the accumulated error in LL is at most 784982538.7×101278498 \cdot 2^{-53} \approx 8.7 \times 10^{-12}, which propagates to an absolute error of 1011\sim 10^{-11} in EE. This is well within the tolerance for the 15th decimal digit. \square

Editorial

Compute floor(E(3, 10^6) * 10^15) where E(3, N) = prod_{p <= N} (1 - p^{-3})^{-1}. The Euler product for zeta(s) converges to Apery’s constant zeta(3) = 1.2020569… We accumulate the product in log-space for numerical stability: L = sum_{p <= N} -ln(1 - p^{-3}) E = exp(L) Key identity: zeta(s) = prod_p (1 - p^{-s})^{-1} (Re(s) > 1) Error bound: zeta(3) - E(3,N) ~ 4*zeta(3) / (N^2 * ln(N)). We sieve of Eratosthenes. We then log-space accumulation. Finally, recover product and extract answer.

Pseudocode

Sieve of Eratosthenes
Log-space accumulation
Recover product and extract answer

Complexity Analysis

  • Time: O(NloglogN)O(N \log \log N) for the Sieve of Eratosthenes, plus O(π(N))=O(N/lnN)O(\pi(N)) = O(N/\ln N) for the log-space accumulation. Total: O(NloglogN)O(N \log \log N).
  • Space: O(N)O(N) for the sieve array.

For N=106N = 10^6: the sieve dominates at 106\sim 10^6 operations; the product accumulation over 7849878498 primes is negligible.

Answer

495568995495726\boxed{495568995495726}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_951/solution.cpp
/*
 * Problem 951: Euler Product Approximation
 *
 * Compute floor(E(3, 10^6) * 10^15) where
 *   E(3, N) = prod_{p <= N, p prime} (1 - p^{-3})^{-1}
 *
 * Algorithm:
 *   1. Sieve primes up to N = 10^6
 *   2. Accumulate L = sum -ln(1 - p^{-3}) in log-space (long double)
 *   3. E = exp(L), answer = floor(E * 10^15)
 *
 * The partial Euler product converges to zeta(3) = 1.20205690315959...
 * (Apery's constant) with error O(1/(N^2 ln N)).
 *
 * Complexity: O(N log log N) for sieve, O(pi(N)) for product.
 */

#include <bits/stdc++.h>
using namespace std;

int main() {
    const int N = 1000000;

    // Sieve of Eratosthenes
    vector<bool> is_prime(N + 1, true);
    is_prime[0] = is_prime[1] = false;
    for (int i = 2; (long long)i * i <= N; i++) {
        if (is_prime[i]) {
            for (int j = i * i; j <= N; j += i) {
                is_prime[j] = false;
            }
        }
    }

    // Accumulate Euler product in log-space using long double
    long double log_prod = 0.0L;
    int prime_count = 0;
    for (int p = 2; p <= N; p++) {
        if (is_prime[p]) {
            long double term = -logl(1.0L - powl((long double)p, -3.0L));
            log_prod += term;
            prime_count++;
        }
    }

    long double E = expl(log_prod);

    // Verification: zeta(3) = 1.2020569031595942854...
    long double zeta3 = 1.2020569031595942854L;
    long double error = zeta3 - E;

    cout << fixed << setprecision(18);
    cout << "E(3, " << N << ") = " << (double)E << endl;
    cout << "zeta(3)     = " << (double)zeta3 << endl;
    cout << "Error       = " << (double)error << endl;
    cout << "Primes used = " << prime_count << endl;
    cout << endl;

    // Final answer
    long long answer = (long long)(E * 1e15L);
    cout << answer << endl;

    return 0;
}