Prime Cube Partnership
There are some primes p for which there exists a positive integer n such that the expression n^3 + n^2 p is a perfect cube. How many primes below one million satisfy this property?
Problem Statement
This archive keeps the full statement, math, and original media on the page.
There are some prime values, \(p\), for which there exists a positive integer, \(n\), such that the expression \(n^3 + n^2p\) is a perfect cube.<
For example, when \(p = 19\), \(8^3 + 8^2 \times 19 = 12^3\).
What is perhaps most surprising is that for each prime with this property the value of \(n\) is unique, and there are only four such primes below one-hundred.
How many primes below one million have this remarkable property?
Problem 131: Prime Cube Partnership
Mathematical Development
Theorem 1 (Characterization). A prime satisfies for some positive integers if and only if is a difference of consecutive cubes, i.e., for some positive integer .
Proof. () Suppose for positive integers . Factor the left-hand side:
Since (as ), write where and . Since is a positive integer and (as ), set for a positive integer . Then , and substituting into (1):
Canceling yields , whence
Since is prime, , and , we consider two cases:
- Case 1: and . By the factorization , and since for , the primality of forces . Hence and
- Case 2: and . This requires and , contradicting .
() Conversely, suppose for some positive integer . Set . Then:
which is a perfect cube with .
Lemma 1 (Search Bound). The condition is equivalent to .
Proof. The quadratic gives . Since must be a positive integer, . Verification: and .
Editorial
A prime p satisfies n^3 + n^2*p = k^3 for some positive integer n if and only if p = (s+1)^3 - s^3 = 3s^2 + 3s + 1 for some positive integer s. Count such primes below 10^6.
Pseudocode
sieve primes up to N
count = 0
for s = 1, 2, ...:
p = 3*s^2 + 3*s + 1
If p >= N then stop this loop
if is_prime(p): count += 1
Return count
Complexity Analysis
- Time: The loop runs iterations (by Lemma 1, ). With a precomputed sieve of size , each primality test is . The sieve costs . Total: .
- Space: for the Boolean sieve array.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
/*
* Project Euler Problem 131: Prime Cube Partnership
*
* A prime p satisfies n^3 + n^2*p = k^3 iff p = (s+1)^3 - s^3
* for some positive integer s. Count such primes below 10^6.
*/
#include <bits/stdc++.h>
using namespace std;
int main() {
const int LIMIT = 1000000;
vector<bool> is_prime(LIMIT, true);
is_prime[0] = is_prime[1] = false;
for (int i = 2; i * i < LIMIT; i++)
if (is_prime[i])
for (int j = i * i; j < LIMIT; j += i)
is_prime[j] = false;
int count = 0;
for (long long s = 1; ; s++) {
long long p = 3 * s * s + 3 * s + 1;
if (p >= LIMIT) break;
if (is_prime[p]) count++;
}
cout << count << endl;
return 0;
}
"""
Project Euler Problem 131: Prime Cube Partnership
A prime p satisfies n^3 + n^2*p = k^3 for some positive integer n if and only
if p = (s+1)^3 - s^3 = 3s^2 + 3s + 1 for some positive integer s.
Count such primes below 10^6.
"""
def sieve(limit):
is_prime = [True] * limit
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, i):
is_prime[j] = False
return is_prime
LIMIT = 1_000_000
is_prime = sieve(LIMIT)
count = 0
s = 1
while True:
p = 3 * s * s + 3 * s + 1
if p >= LIMIT:
break
if is_prime[p]:
count += 1
s += 1
print(count)