Large Non-Mersenne Prime
In 2004, a massive non-Mersenne prime was discovered: N = 28433 x 2^7830457 + 1. Find the last ten digits of N.
Problem Statement
This archive keeps the full statement, math, and original media on the page.
The first known prime found to exceed one million digits was discovered in 1999, and is a Mersenne prime of the form \(2^{6972593} - 1\); it contains exactly \(2\,098\,960\) digits. Subsequently other Mersenne primes, of the form \(2^p - 1\), have been found which contain more digits.
However, in 2004 there was found a massive non-Mersenne prime which contains \(2\,357\,207\) digits: \(28433 \times 2^{7830457} + 1\).
Find the last ten digits of this prime number.
Problem 97: Large Non-Mersenne Prime
Mathematical Foundation
Theorem 1 (Modular reduction is a ring homomorphism). For any positive integer , the canonical projection defined by is a surjective ring homomorphism. In particular, for all :
Proof. Write and with . Then , so . Similarly, , so . These are precisely the ring axioms for .
Corollary 1. For any with :
Theorem 2 (Correctness of binary exponentiation). Let be positive integers with binary representation . The following procedure computes :
Initialize , . For : if , set ; then set .
Proof. We prove by induction on that after iteration , and .
Base case (, before the loop): and . Both invariants hold.
Inductive step: Suppose the invariants hold before iteration . We have (by the invariant from iteration ). If , then by Theorem 1. If , is unchanged and . Then .
After all iterations, .
Theorem 3 (Bit-complexity). Binary exponentiation of performs squarings and at most multiplications, each modulo . If fits in a machine word, each modular multiplication is , giving total time .
Proof. The loop iterates once per bit of , performing one squaring and at most one multiplication per iteration.
Application. With , , and :
The exponent has bits, so the computation requires 23 squarings and at most 23 multiplications modulo . Since , all intermediate products fit in 64-bit arithmetic (using 128-bit intermediate results for the multiplication step).
Editorial
Only the last ten digits matter, so every operation can be performed modulo . That means we never need to build the enormous number itself; we only need its residue modulo the target modulus.
The heavy step is modular exponentiation, and repeated squaring handles that in logarithmic time. After computing , the remaining multiplication by and the final addition of are done under the same modulus. The search space is therefore reduced from a huge integer to a short sequence of modular updates.
Pseudocode
Set the modulus to $10^{10}$.
Compute the residue of $2^{7830457}$ modulo this modulus.
Multiply that residue by $28433$.
Add $1$ and reduce once more modulo $10^{10}$.
Return the resulting last ten digits.
Complexity Analysis
Time: modular multiplications. Each is with fixed-precision arithmetic. Total: .
Space: .
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() {
// Compute (28433 * 2^7830457 + 1) mod 10^10
// Using binary exponentiation with __int128 to avoid overflow.
const long long mod = 10000000000LL;
auto mulmod = [](long long a, long long b, long long m) -> long long {
return ((__int128)a * b) % m;
};
// Binary exponentiation: 2^7830457 mod 10^10
long long base = 2, exp = 7830457, result = 1;
base %= mod;
while (exp > 0) {
if (exp & 1)
result = mulmod(result, base, mod);
base = mulmod(base, base, mod);
exp >>= 1;
}
result = mulmod(28433LL, result, mod);
result = (result + 1) % mod;
cout << result << endl;
return 0;
}
"""
Project Euler Problem 97: Large Non-Mersenne Prime
Find the last ten digits of 28433 * 2^7830457 + 1.
Uses Python's built-in three-argument pow(base, exp, mod) which implements
binary exponentiation (repeated squaring) in O(log exp) modular multiplications.
Answer: 8739992577
"""
def main():
mod = 10**10
result = (28433 * pow(2, 7830457, mod) + 1) % mod
print(result)
if __name__ == "__main__":
main()