All Euler problems
Project Euler

Goldbach Partition Counting

For an even number n >= 4, let G(n) be the number of ways to write n = p + q where p <= q are both prime. Find sum_(n=4, n even)^(10^6) G(n).

Source sync Apr 19, 2026
Problem #0939
Level Level 38
Solved By 142
Languages C++, Python
Answer 246776732
Length 291 words
number_theoryoptimizationanalytic_math

Problem Statement

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

Two players A and B are playing a variant of Nim.

At the beginning, there are several piles of stones. Each pile is either at the side of A or at the side of B. The piles are unordered.

They make moves in turn. At a player's turn, the player can

  • either choose a pile on the opponent's side and remove one stone from that pile;

  • or choose a pile on their own side and remove the whole pile.

The winner is the player who removes the last stone.

Let $E(N)$ be the number of initial settings with at most $N$ stones such that, whoever plays first, A always has a winning strategy.

For example $E(4) = 9$; the settings are:

Nr.Piles at the side of APiles at the side of B
1$4$none
2$1, 3$none
3$2, 2$none
4$1, 1, 2$none
5$3$$1$
6$1, 2$$1$
7$2$$1, 1$
8$3$none
9$2$none

Find $E(5000) \bmod 1234567891$.

Problem 939: Goldbach Partition Counting

Mathematical Foundation

Definition. For even n4n \geq 4, the Goldbach partition function is:

G(n)=#{(p,q):p+q=n,  pq,  p,q prime}.G(n) = \#\{(p, q) : p + q = n,\; p \leq q,\; p, q \text{ prime}\}.

Theorem 1 (Goldbach’s conjecture — computational verification). Every even integer nn with 4n4×10184 \leq n \leq 4 \times 10^{18} satisfies G(n)1G(n) \geq 1.

Proof. This has been verified computationally (Oliveira e Silva, 2013, verified up to 4×10184 \times 10^{18}). For our range n106n \leq 10^6, the verification is trivial by direct computation. \square

Theorem 2 (Sieve-based computation of G(n)G(n)). Let P\mathcal{P} denote the set of primes. Then:

G(n)=#{pP:pn/2,  npP}.G(n) = \#\{p \in \mathcal{P} : p \leq n/2,\; n - p \in \mathcal{P}\}.

Proof. Each unordered pair {p,q}\{p, q\} with p+q=np + q = n and pqp \leq q corresponds uniquely to a prime pn/2p \leq n/2 such that q=npq = n - p is also prime. The constraint pqp \leq q is equivalent to pn/2p \leq n/2. \square

Lemma 1 (Counting identity). The total n=4n evenNG(n)\sum_{\substack{n=4 \\ n \text{ even}}}^{N} G(n) counts the number of unordered pairs of primes (p,q)(p, q) with pqp \leq q and p+qNp + q \leq N even. Equivalently, it counts all pairs of primes summing to an even number at most NN, with pqp \leq q.

Proof. Each pair (p,q)(p, q) with pqp \leq q both prime and p+q=np + q = n even is counted exactly once in G(n)G(n). Summing over all even nn from 4 to NN collects all such pairs. Note that p+qp + q is even iff both are odd or both equal 2; since 2 is the only even prime, the only pair with p=q=2p = q = 2 gives n=4n = 4, and all other pairs have p=2p = 2 (giving odd q=n2q = n - 2, so nn is even) or both p,qp, q odd (giving even sum). \square

Theorem 3 (Hardy—Littlewood conjecture, asymptotic). For even nn \to \infty:

G(n)2C2n(lnn)2pnp>2p1p2,G(n) \sim \frac{2 C_2 \, n}{(\ln n)^2} \prod_{\substack{p \mid n \\ p > 2}} \frac{p - 1}{p - 2},

where C2=p>2(11(p1)2)0.6602C_2 = \prod_{p > 2}\left(1 - \frac{1}{(p-1)^2}\right) \approx 0.6602 is the twin prime constant.

Proof. This is a heuristic conjecture based on the Hardy—Littlewood circle method. A rigorous proof remains open. The formula gives excellent empirical agreement for large nn. \square

Editorial

Optimized approach:*. We sieve of Eratosthenes up to N. We then iterate over each even n, count G(n) and accumulate. Finally, iterate over n from 4 to N step 2.

Pseudocode

Sieve of Eratosthenes up to N
For each even n, count G(n) and accumulate
for n from 4 to N step 2
for p from 2 to n/2
Sieve primes
For each prime p, count even n in [2p, N] such that n - p is prime
Equivalently: for each pair (p, q) with p <= q, p + q <= N, p + q even

Complexity Analysis

  • Time (naive): O ⁣(n=4n evenNn2)=O(N2)O\!\left(\sum_{\substack{n=4 \\ n \text{ even}}}^{N} \frac{n}{2}\right) = O(N^2).
  • Time (optimized): O(π(N/2)2)O ⁣(N2ln2N)O(\pi(N/2)^2) \approx O\!\left(\frac{N^2}{\ln^2 N}\right), which is a constant-factor improvement.
  • Time (sieve): O(NloglogN)O(N \log \log N) for the Eratosthenes sieve.
  • Space: O(N)O(N) for the primality array.

Answer

246776732\boxed{246776732}

Code

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

C++ project_euler/problem_939/solution.cpp
#include <bits/stdc++.h>
using namespace std;
int main(){
    const int N=100000;
    vector<bool> sieve(N+1,true);
    sieve[0]=sieve[1]=false;
    for(int i=2;i*i<=N;i++) if(sieve[i]) for(int j=i*i;j<=N;j+=i) sieve[j]=false;
    long long total=0;
    for(int n=4;n<=N;n+=2){
        int cnt=0;
        for(int p=2;p<=n/2;p++) if(sieve[p]&&sieve[n-p]) cnt++;
        total+=cnt;
    }
    cout<<total<<endl;
    return 0;
}