The Project Euler prime cube partnership problem states:
"There are some prime values, p, for which there exists a positive integer, n, such that the expression is a perfect cube.
For example, when
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?"
To solve this problem, let’s start with the following equation:
In the equation above, is the perfect prime and is prime.
Now, let’s simplify this equation:
From the calculation above, we can conclude that if is an integer, then and must be perfect cubes because both terms are under the cube root formula.
Now, if both and are perfect cubes, then we can replace both of them with new terms, as shown below:
Both and belong to the integer set.
Also,
The difference between cube formulas can also be written as follows:
From the equation above, we can see that the term is a factor of , which means is divisible by . However, if we want to be prime, then must be equal to 1.
This indicates that and are two consecutive numbers because their difference is 1.
We can rewrite the equation above as follows:
Now, we only need to check the following equation for which value of is below one million (1000000).
For , we get , which is greater than 1 million.
To write the code, we only need to check the values up to 577 because values greater than 577 do not satisfy the equation.
We can find the solution with the following code:
#include <iostream>using namespace std;bool isprime(int number) {bool ans = true;if (number == 0 || number == 1) {ans = false;}else {for (int i = 2; i <= number / 2; ++i) {if (number % i == 0) {ans = false;break;}}}return ans;}int main() {int res = 0;for (int i = 1; i < 577; i++) {int cube1 = (i + 1) * (i + 1) * (i + 1);int cube2 = i * i * i;if (isprime( cube1 - cube2))res++;}cout << res << endl;return 0;}
The code above gives the answer as the number of primes with this property.