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.
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 be prime and . Then:
- If is even: .
- If is odd: .
Proof. By the binomial theorem, expanding modulo (all terms with for vanish):
Adding these two expressions:
Case 1 ( even): and , so
Case 2 ( odd): and , so
Lemma 1 (No modular reduction needed). For all odd , , so the remainder equals exactly .
Proof. The inequality is equivalent to . By Bertrand’s postulate applied iteratively, for all . Explicitly: , , and for , since the prime gap is positive and grows faster than by the Prime Number Theorem (). For the small odd cases : , ; , … Actually, , so . But for our problem we need , which requires large , so the inequality certainly holds in the relevant range.
Theorem 2 (Characterization of the answer). For even , always. For odd in the range where , is strictly increasing in . The answer is the smallest odd satisfying .
Proof. For even , by Theorem 1. For odd with , . Since both and are increasing functions of , the product is strictly increasing. We therefore perform a linear search over odd .
Editorial
The binomial expansion removes almost all of the arithmetic. Once the remainder formula is simplified modulo , every even index gives the useless constant value 2, while every relevant odd index behaves like . 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 directly. The first odd index where this value exceeds 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: for the Sieve of Eratosthenes with (sufficient since ), plus for the linear scan where .
- Space: for the sieve boolean array and extracted prime list.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#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;
}
"""
Problem 123: Prime Square Remainders
Find least n such that ((p_n-1)^n + (p_n+1)^n) mod p_n^2 > 10^10.
For odd n, remainder = 2*n*p_n. For even n, remainder = 2.
Search for smallest odd n with 2*n*p_n > 10^10.
"""
def sieve(limit):
is_prime = [True] * (limit + 1)
is_prime[0] = is_prime[1] = False
for i in range(2, int(limit**0.5) + 1):
if is_prime[i]:
for j in range(i * i, limit + 1, i):
is_prime[j] = False
return [i for i in range(2, limit + 1) if is_prime[i]]
def solve():
threshold = 10**10
primes = sieve(300000)
for i, p in enumerate(primes):
n = i + 1
if n % 2 == 0:
continue
if 2 * n * p > threshold:
print(n)
return
solve()