Open Source · 7 Languages · 900+ Problems

Project Euler Solutions

Complete solutions in C++, Python & Java — with step-by-step mathematical explanations

All Problems

Problem 518: Prime Triples and Geometric Sequences

View on Project Euler

Project Euler Problem 518 Solution

EulerSolve provides an optimized solution for Project Euler Problem 518, Prime Triples and Geometric Sequences, with C++, Python, Java, and a step-by-step mathematical explanation.

Problem Summary We must sum \(a+b+c\) over all prime triples \((a,b,c)\) with \(a<b<c<n\) such that \(a+1\), \(b+1\), and \(c+1\) form a geometric progression. If we write $x=a+1,\qquad y=b+1,\qquad z=c+1,$ then the defining condition is \(y^2=xz\). A naive search over all prime triples below \(n\) is infeasible for the real input, so the implementations enumerate the hidden arithmetic structure instead of scanning primes directly. Mathematical Approach The key idea is to classify every valid triple by a common scale factor and a coprime pair that describes the geometric progression exactly. Step 1: Convert the geometric progression into one Diophantine equation Three positive numbers are in geometric progression exactly when the square of the middle term equals the product of the outer terms. Therefore $\frac{b+1}{a+1}=\frac{c+1}{b+1}\iff (b+1)^2=(a+1)(c+1).$ With \(x=a+1\), \(y=b+1\), and \(z=c+1\), the problem becomes $y^2=xz,\qquad x<y<z.$ So we no longer have to reason about geometric progressions directly; we only need a complete description of the positive integer solutions to \(y^2=xz\). Step 2: Parameterize all integer solutions Let $d=\gcd(x,z),\qquad x=d r,\qquad z=d s,\qquad \gcd(r,s)=1.$ Substituting into \(y^2=xz\) gives $y^2=d^2rs.$ Hence \(rs\) must be a perfect square....

Detailed mathematical approach

Problem Summary

We must sum \(a+b+c\) over all prime triples \((a,b,c)\) with \(a<b<c<n\) such that \(a+1\), \(b+1\), and \(c+1\) form a geometric progression. If we write

$x=a+1,\qquad y=b+1,\qquad z=c+1,$

then the defining condition is \(y^2=xz\). A naive search over all prime triples below \(n\) is infeasible for the real input, so the implementations enumerate the hidden arithmetic structure instead of scanning primes directly.

Mathematical Approach

The key idea is to classify every valid triple by a common scale factor and a coprime pair that describes the geometric progression exactly.

Step 1: Convert the geometric progression into one Diophantine equation

Three positive numbers are in geometric progression exactly when the square of the middle term equals the product of the outer terms. Therefore

$\frac{b+1}{a+1}=\frac{c+1}{b+1}\iff (b+1)^2=(a+1)(c+1).$

With \(x=a+1\), \(y=b+1\), and \(z=c+1\), the problem becomes

$y^2=xz,\qquad x<y<z.$

So we no longer have to reason about geometric progressions directly; we only need a complete description of the positive integer solutions to \(y^2=xz\).

Step 2: Parameterize all integer solutions

Let

$d=\gcd(x,z),\qquad x=d r,\qquad z=d s,\qquad \gcd(r,s)=1.$

Substituting into \(y^2=xz\) gives

$y^2=d^2rs.$

Hence \(rs\) must be a perfect square. Since \(r\) and \(s\) are coprime, each of them must already be a perfect square, so we may write

$r=u^2,\qquad s=v^2,\qquad \gcd(u,v)=1.$

Therefore every solution has the form

$x=du^2,\qquad y=duv,\qquad z=dv^2,$

and every positive choice of \(d,u,v\) with \(\gcd(u,v)=1\) satisfies \(y^2=xz\). Because \(x<z\), we only need \(u<v\).

Step 3: Translate back to the prime triple

Undoing the substitution yields

$a=du^2-1,\qquad b=duv-1,\qquad c=dv^2-1.$

Since \(u<v\), we automatically get

$du^2<duv<dv^2,$

so the ordering \(a<b<c\) is built into the parameterization. The coprimality condition \(\gcd(u,v)=1\) removes duplicate descriptions, which means each valid prime triple corresponds to one unique search state \((d,u,v)\).

Step 4: Use parity to isolate the only odd-\(d\) case

If \(a\), \(b\), and \(c\) are all odd primes, then \(a+1\), \(b+1\), and \(c+1\) are all even, so \(x\), \(y\), and \(z\) are even. In the parameterization above that forces \(d\) to be even, because with odd \(d\) the coprime pair \(u,v\) cannot make all three numbers \(du^2\), \(duv\), and \(dv^2\) even.

Now suppose \(d\) is odd. Since \(b\) is a prime greater than \(2\), \(b+1=y\) is even, so \(uv\) must be even. Because \(\gcd(u,v)=1\), exactly one of \(u\) and \(v\) is even.

The largest prime \(c\) is also greater than \(2\), hence \(c+1=z\) must be even. Therefore \(v\) must be even and \(u\) must be odd. But then \(x=du^2\) is odd, so \(a=x-1\) is even. The only even prime is \(2\), so

$a=2,\qquad x=a+1=3.$

Thus

$du^2=3,$

which forces

$u=1,\qquad d=3.$

So the odd-\(d\) part of the search is not a general family at all: it is the single special branch

$a=2,\qquad b=3v-1,\qquad c=3v^2-1,$

with \(v\) even.

Step 5: Bound the finite search region

From the largest value we get

$c=dv^2-1<n,$

so, because everything is integral,

$dv^2\le n,\qquad d\le \left\lfloor\frac{n}{v^2}\right\rfloor.$

Since \(d\ge 1\), this also implies

$v^2\le n,\qquad v\le \lfloor\sqrt{n}\rfloor.$

Therefore the search can be organized as: loop over \(v\), loop over coprime \(u<v\), and then loop over all admissible scale factors \(d\). The main branch uses even \(d\); the only odd branch is \(d=3\), \(u=1\), \(v\) even.

Worked Example: \(n=100\)

The special odd branch already gives one valid triple. Taking

$u=1,\qquad v=2,\qquad d=3$

produces

$a=2,\qquad b=5,\qquad c=11.$

From the even branch, for example,

$u=1,\qquad v=2,\qquad d=6\Rightarrow (a,b,c)=(5,11,23),$

and

$u=2,\qquad v=3,\qquad d=2\Rightarrow (a,b,c)=(7,11,17).$

Continuing the enumeration below \(100\) gives exactly 11 triples:

\((2,5,11)\), \((2,11,47)\), \((5,11,23)\), \((5,17,53)\), \((7,11,17)\), \((7,23,71)\), \((11,23,47)\), \((17,23,31)\), \((17,41,97)\), \((31,47,71)\), \((71,83,97)\).

Their total is

$18+60+39+75+35+101+81+71+155+149+251=1035,$

which matches the checkpoint used by the implementation.

How the Code Works

The C++, Python, and Java implementations all follow the same plan. First they build an odd-only prime table up to \(n\), so every primality test becomes a constant-time table lookup for odd numbers, plus one explicit check for \(2\).

Next they loop over \(v=2,3,\dots,\lfloor\sqrt{n}\rfloor\). For each \(v\), they compute \(v^2\) and the largest allowable scale factor \(\left\lfloor n/v^2 \right\rfloor\). Then they scan all \(u\) with \(1\le u<v\), keeping only the coprime pairs.

For each such pair, the implementation first checks the unique odd-\(d\) branch \(d=3\), \(u=1\), \(v\) even. After that it runs through all even values of \(d\) up to the bound, forms

$a=du^2-1,\qquad b=duv-1,\qquad c=dv^2-1,$

and adds \(a+b+c\) whenever all three numbers are prime.

The C++ implementation also performs two short internal validations before the full run: the known value \(S(100)=1035\), and a fast-versus-direct comparison at \(n=500\). The Python and Java implementations keep the same optimized search structure without the extra direct checker.

Complexity Analysis

Building the odd-only sieve up to \(n\) costs \(O(n\log\log n)\) time and \(O(n)\) memory.

The search phase examines

$\sum_{v\le \sqrt{n}}\sum_{\substack{1\le u<v\\ \gcd(u,v)=1}} O\!\left(\frac{n}{v^2}\right)$

candidate scale factors, which is \(O(n\log n)\) overall. The gcd checks fit within the same order, so the enumeration is dominated by arithmetic generation rather than by primality testing. The full method therefore runs in \(O(n\log n)\) time and uses \(O(n)\) memory.

Footnotes and References

  1. Problem page: https://projecteuler.net/problem=518
  2. Geometric progression: Wikipedia — Geometric progression
  3. Coprime integers: Wikipedia — Coprime integers
  4. Sieve of Eratosthenes: Wikipedia — Sieve of Eratosthenes

Mathematical approach · C++ solution · Python solution · Java solution

Previous: Problem 517 · All Project Euler solutions · Next: Problem 519

Need help with a problem? Ask me! 💡
e
✦ Euler GLM 5.2
Hi! I'm Euler. Ask me anything about Project Euler problems, math concepts, or solution approaches! 🧮