All Euler problems
Project Euler

Prime Square Remainders

Let p_n denote the n -th prime (p_1 = 2, p_2 = 3, p_3 = 5, etc.). Define r_n = [(p_n - 1)^n + (p_n + 1)^n] mod p_n^2. Find the least value of n for which r_n first exceeds 10^10.

Source sync May 21, 2026
Problem #0123
Level Level 04
Solved By 13,026
Languages C++, Python
Answer 21035
Length 305 words
number_theorymodular_arithmeticlinear_algebra

Problem Statement

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

Let \(p_n\) be the \(n\)th prime: \(2, 3, 5, 7, 11, \dots \), and let \(r\) be the remainder when \((p_n - 1)^n + (p_n + 1)^n\) is divided by \(p_n^2\).

For example, when \(n = 3\), \(p_3 = 5\), and \(4^3 + 6^3 = 280 \equiv 5 \mod 25\).

The least value of \(n\) for which the remainder first exceeds \(10^9\) is \(7037\).

Find the least value of \(n\) for which the remainder first exceeds \(10^{10}\).

Problem 123: Prime Square Remainders

Mathematical Development

Theorem 1 (Closed-form remainder). Let p=pnp = p_n be prime and n≥1n \ge 1. Then:

  • If nn is even: rn=2r_n = 2.
  • If nn is odd: rn≡2np(modp2)r_n \equiv 2np \pmod{p^2}.

Proof. By the binomial theorem, expanding modulo p2p^2 (all terms with pkp^k for k≥2k \ge 2 vanish):

(p+1)n=∑k=0n(nk)pk≡1+np(modp2),(p + 1)^n = \sum_{k=0}^{n} \binom{n}{k} p^k \equiv 1 + np \pmod{p^2}, (p−1)n=∑k=0n(nk)pk(−1)n−k≡(−1)n+np(−1)n−1(modp2).(p - 1)^n = \sum_{k=0}^{n} \binom{n}{k} p^k (-1)^{n-k} \equiv (-1)^n + n p (-1)^{n-1} \pmod{p^2}.

Adding these two expressions:

Case 1 (nn even): (−1)n=1(-1)^n = 1 and (−1)n−1=−1(-1)^{n-1} = -1, so

(p−1)n+(p+1)n≡(1−np)+(1+np)=2(modp2).(p-1)^n + (p+1)^n \equiv (1 - np) + (1 + np) = 2 \pmod{p^2}.

Case 2 (nn odd): (−1)n=−1(-1)^n = -1 and (−1)n−1=1(-1)^{n-1} = 1, so

(p−1)n+(p+1)n≡(−1+np)+(1+np)=2np(modp2).□(p-1)^n + (p+1)^n \equiv (-1 + np) + (1 + np) = 2np \pmod{p^2}. \quad \square

Lemma 1 (No modular reduction needed). For all odd n≥5n \ge 5, 2npn<pn22np_n < p_n^2, so the remainder equals exactly rn=2npnr_n = 2np_n.

Proof. The inequality 2npn<pn22np_n < p_n^2 is equivalent to 2n<pn2n < p_n. By Bertrand’s postulate applied iteratively, pn>2np_n > 2n for all n≥5n \ge 5. Explicitly: p5=11>10p_5 = 11 > 10, p6=13>12p_6 = 13 > 12, and for n≥6n \ge 6, pn≥p6+(n−6)>2np_n \ge p_6 + (n - 6) > 2n since the prime gap is positive and pnp_n grows faster than 2n2n by the Prime Number Theorem (pn∼nln⁡np_n \sim n \ln n). For the small odd cases n=1,3n = 1, 3: p1=2p_1 = 2, r1=(1+3) mod 4=0r_1 = (1 + 3) \bmod 4 = 0; p3=5p_3 = 5, r3=2⋅3⋅5=30<25r_3 = 2 \cdot 3 \cdot 5 = 30 < 25… Actually, 2⋅3⋅5=30>25=p322 \cdot 3 \cdot 5 = 30 > 25 = p_3^2, so r3=30 mod 25=5r_3 = 30 \bmod 25 = 5. But for our problem we need rn>1010r_n > 10^{10}, which requires large nn, so the inequality 2n<pn2n < p_n certainly holds in the relevant range. □\square

Theorem 2 (Characterization of the answer). For even nn, rn=2≤1010r_n = 2 \le 10^{10} always. For odd nn in the range where 2n<pn2n < p_n, rn=2npnr_n = 2np_n is strictly increasing in nn. The answer is the smallest odd nn satisfying 2npn>10102np_n > 10^{10}.

Proof. For even nn, rn=2r_n = 2 by Theorem 1. For odd nn with 2n<pn2n < p_n, rn=2npnr_n = 2np_n. Since both nn and pnp_n are increasing functions of nn, the product 2npn2np_n is strictly increasing. We therefore perform a linear search over odd nn. □\square

Editorial

The binomial expansion removes almost all of the arithmetic. Once the remainder formula is simplified modulo pn2p_n^2, every even index gives the useless constant value 2, while every relevant odd index behaves like 2npn2np_n. That means the problem is not really about modular arithmetic anymore; it is just a monotone threshold search over the prime sequence.

The implementation therefore sieves enough primes in advance, scans only odd indices, and evaluates the expression 2npn2np_n directly. The first odd index where this value exceeds 101010^{10} is the answer.

Pseudocode

Generate all primes up to a safe upper bound.

Inspect the prime indices in increasing order, but skip the even indices because their remainder is always 2.
For each odd index n:
    Read the n-th prime p_n from the sieve output.
    If 2 n p_n exceeds the threshold, return n.

Complexity Analysis

  • Time: O(Plog⁡log⁡P)O(P \log \log P) for the Sieve of Eratosthenes with P=300,000P = 300{,}000 (sufficient since p21035=237,737<300,000p_{21035} = 237{,}737 < 300{,}000), plus O(n∗)O(n^*) for the linear scan where n∗=21,035n^* = 21{,}035.
  • Space: O(P)O(P) for the sieve boolean array and extracted prime list.

Answer

21035\boxed{21035}

Code

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

C++ project_euler/problem_123/solution.cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    const int LIMIT = 300000;
    vector<bool> is_prime(LIMIT + 1, true);
    is_prime[0] = is_prime[1] = false;
    for (int i = 2; (long long)i * i <= LIMIT; i++)
        if (is_prime[i])
            for (int j = i * i; j <= LIMIT; j += i)
                is_prime[j] = false;

    vector<int> primes;
    for (int i = 2; i <= LIMIT; i++)
        if (is_prime[i]) primes.push_back(i);

    const long long threshold = 10000000000LL;

    for (int i = 0; i < (int)primes.size(); i++) {
        int n = i + 1;
        if (n % 2 == 0) continue;
        long long p = primes[i];
        if (2LL * n * p > threshold) {
            cout << n << endl;
            return 0;
        }
    }
    return 0;
}