All Euler problems
Project Euler

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?

Source sync Apr 19, 2026
Problem #0131
Level Level 05
Solved By 8,488
Languages C++, Python
Answer 173
Length 235 words
number_theoryalgebrabrute_force

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 pp satisfies n3+n2p=k3n^3 + n^2 p = k^3 for some positive integers n,kn, k if and only if pp is a difference of consecutive cubes, i.e., p=(s+1)3s3p = (s+1)^3 - s^3 for some positive integer ss.

Proof. (\Rightarrow) Suppose n3+n2p=k3n^3 + n^2 p = k^3 for positive integers n,kn, k. Factor the left-hand side:

n2(n+p)=k3.(1)n^2(n + p) = k^3. \tag{1}

Since k>nk > n (as k3=n3+n2p>n3k^3 = n^3 + n^2 p > n^3), write k=nr/sk = n \cdot r/s where gcd(r,s)=1\gcd(r, s) = 1 and r>s1r > s \geq 1. Since kk is a positive integer and sns \mid n (as gcd(r,s)=1\gcd(r,s) = 1), set n=s3qn = s^3 q for a positive integer qq. Then k=s2qrk = s^2 q r, and substituting into (1):

s6q2(s3q+p)=s6q3r3.s^6 q^2 (s^3 q + p) = s^6 q^3 r^3.

Canceling s6q2>0s^6 q^2 > 0 yields s3q+p=qr3s^3 q + p = q r^3, whence

p=q(r3s3).(2)p = q(r^3 - s^3). \tag{2}

Since pp is prime, q1q \geq 1, and r3s31r^3 - s^3 \geq 1, we consider two cases:

  • Case 1: q=1q = 1 and r3s3=pr^3 - s^3 = p. By the factorization r3s3=(rs)(r2+rs+s2)r^3 - s^3 = (r - s)(r^2 + rs + s^2), and since r2+rs+s21+1+1=3>1r^2 + rs + s^2 \geq 1 + 1 + 1 = 3 > 1 for r,s1r, s \geq 1, the primality of pp forces rs=1r - s = 1. Hence r=s+1r = s + 1 and
p=(s+1)3s3=3s2+3s+1.p = (s+1)^3 - s^3 = 3s^2 + 3s + 1.
  • Case 2: r3s3=1r^3 - s^3 = 1 and q=pq = p. This requires r=1r = 1 and s=0s = 0, contradicting s1s \geq 1.

(\Leftarrow) Conversely, suppose p=(s+1)3s3p = (s+1)^3 - s^3 for some positive integer ss. Set n=s3n = s^3. Then:

n3+n2p=s9+s6[(s+1)3s3]=s6(s+1)3=[s2(s+1)]3,n^3 + n^2 p = s^9 + s^6\bigl[(s+1)^3 - s^3\bigr] = s^6(s+1)^3 = \bigl[s^2(s+1)\bigr]^3,

which is a perfect cube with k=s2(s+1)k = s^2(s+1). \square

Lemma 1 (Search Bound). The condition p=3s2+3s+1<106p = 3s^2 + 3s + 1 < 10^6 is equivalent to s577s \leq 577.

Proof. The quadratic 3s2+3s+1<1063s^2 + 3s + 1 < 10^6 gives s<3+9+12(1061)6577.35s < \frac{-3 + \sqrt{9 + 12(10^6 - 1)}}{6} \approx 577.35. Since ss must be a positive integer, s577s \leq 577. Verification: 3(577)2+3(577)+1=998,788<1063(577)^2 + 3(577) + 1 = 998{,}788 < 10^6 and 3(578)2+3(578)+1=1,002,469>1063(578)^2 + 3(578) + 1 = 1{,}002{,}469 > 10^6. \square

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 O(N)O(\sqrt{N}) iterations (by Lemma 1, s=O(N)s = O(\sqrt{N})). With a precomputed sieve of size NN, each primality test is O(1)O(1). The sieve costs O(NloglogN)O(N \log\log N). Total: O(NloglogN)O(N \log\log N).
  • Space: O(N)O(N) for the Boolean sieve array.

Answer

173\boxed{173}

Code

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

C++ project_euler/problem_131/solution.cpp
/*
 * 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;
}