Open Source · 7 Languages · 900+ Problems

Project Euler Solutions

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

All Problems

Problem 510: Tangent Circles

View on Project Euler

Project Euler Problem 510 Solution

EulerSolve provides an optimized solution for Project Euler Problem 510, Tangent Circles, with C++, Python, Java, and a step-by-step mathematical explanation.

Problem Summary Let \(S(N)\) denote the sum of \(a+b+c\) over all integer triples \((a,b,c)\) with \(1\le a\le b\le N\) such that $\sqrt{ab}\in \mathbb{Z},\qquad c=\frac{ab}{a+b+2\sqrt{ab}}\in \mathbb{Z}_{>0}.$ The tangent-circle geometry is already condensed into this arithmetic condition. The difficulty is that \(a\) and \(b\) are tied together by both a square-root constraint and a divisibility constraint, so scanning all pairs up to \(N=10^9\) is far too slow. The key is to show that every valid triple comes from one coprime parameter pair and one scale factor. Mathematical Approach The solution derives a complete parameterization and then sums one whole family at a time. Step 1: Separate the Square Part of \(ab\) Write $a=g u,\qquad b=g v,\qquad \gcd(u,v)=1,$ where \(g=\gcd(a,b)\). Because \(\sqrt{ab}\) is an integer, the product \(ab=g^2uv\) is a perfect square, so \(uv\) is also a perfect square....

Detailed mathematical approach

Problem Summary

Let \(S(N)\) denote the sum of \(a+b+c\) over all integer triples \((a,b,c)\) with \(1\le a\le b\le N\) such that

$\sqrt{ab}\in \mathbb{Z},\qquad c=\frac{ab}{a+b+2\sqrt{ab}}\in \mathbb{Z}_{>0}.$

The tangent-circle geometry is already condensed into this arithmetic condition. The difficulty is that \(a\) and \(b\) are tied together by both a square-root constraint and a divisibility constraint, so scanning all pairs up to \(N=10^9\) is far too slow. The key is to show that every valid triple comes from one coprime parameter pair and one scale factor.

Mathematical Approach

The solution derives a complete parameterization and then sums one whole family at a time.

Step 1: Separate the Square Part of \(ab\)

Write

$a=g u,\qquad b=g v,\qquad \gcd(u,v)=1,$

where \(g=\gcd(a,b)\). Because \(\sqrt{ab}\) is an integer, the product \(ab=g^2uv\) is a perfect square, so \(uv\) is also a perfect square. Since \(u\) and \(v\) are coprime, each of them must already be a square:

$u=m^2,\qquad v=n^2,\qquad \gcd(m,n)=1.$

Therefore every valid pair can be written as

$a=g m^2,\qquad b=g n^2,\qquad \sqrt{ab}=gmn.$

Step 2: Force the Denominator to Divide Exactly

Substituting the previous form into the definition of \(c\) gives

$c=\frac{g^2m^2n^2}{g(m^2+n^2+2mn)}=\frac{g m^2n^2}{(m+n)^2}.$

Now

$\gcd(m+n,m)=\gcd(m+n,n)=1,$

so \((m+n)\) is coprime to both \(m\) and \(n\), hence

$\gcd\left((m+n)^2,m^2n^2\right)=1.$

That means the entire denominator \((m+n)^2\) must come from the common factor \(g\). So there is a positive integer \(t\) with

$g=t(m+n)^2.$

We obtain the exact shape of every valid triple:

$a=t(m+n)^2m^2,\qquad b=t(m+n)^2n^2,\qquad c=t m^2n^2.$

Step 3: Show the Parameterization Is Complete and Unique

The converse is immediate. If \(\gcd(m,n)=1\) and \(t\ge 1\), then

$\sqrt{ab}=t(m+n)^2mn\in \mathbb{Z},$

and

$a+b+2\sqrt{ab}=t(m+n)^2(m^2+n^2+2mn)=t(m+n)^4.$

Therefore

$\frac{ab}{a+b+2\sqrt{ab}}=\frac{t^2(m+n)^4m^2n^2}{t(m+n)^4}=t m^2n^2=c,$

so every triple of this form is valid. The coprime pair \((m,n)\) is unique up to swapping the two entries, so enforcing

$m\le n$

removes double counting. For one coprime pair, the primitive triple is

$a_0=(m+n)^2m^2,\qquad b_0=(m+n)^2n^2,\qquad c_0=m^2n^2.$

Step 4: Reduce the Bound to One Variable

When \(m\le n\), the three components satisfy

$c_0\le a_0\le b_0.$

So after scaling by \(t\), the condition that all components lie under \(N\) reduces to the single inequality

$t b_0\le N.$

Hence the admissible scales are exactly

$1\le t\le t_{\max}=\left\lfloor\frac{N}{b_0}\right\rfloor.$

The entire family generated by one primitive triple contributes

$\sum_{t=1}^{t_{\max}} t(a_0+b_0+c_0)=(a_0+b_0+c_0)\frac{t_{\max}(t_{\max}+1)}{2}.$

Step 5: Enumerate Only the Feasible Coprime Pairs

For fixed \(n\), the quantity \(b_0=(m+n)^2n^2\) increases with \(m\), so once \(b_0>N\) the inner search can stop immediately. The smallest possible \(b_0\) for that \(n\) occurs at \(m=1\):

$b_{0,\min}=(n+1)^2n^2.$

If this already exceeds \(N\), then no larger \(n\) can work. Because \(b_{0,\min}\) is on the order of \(n^4\), only values

$n=O(N^{1/4})$

must be inspected.

Worked Example: \(N=100\)

The feasible coprime pairs with \(m\le n\) are very few.

For \((m,n)=(1,1)\), the primitive triple is

$(a_0,b_0,c_0)=(4,4,1),\qquad t_{\max}=\left\lfloor\frac{100}{4}\right\rfloor=25.$

Its contribution is

$(4+4+1)\frac{25\cdot 26}{2}=9\cdot 325=2925.$

For \((m,n)=(1,2)\), the primitive triple is

$(a_0,b_0,c_0)=(9,36,4),\qquad t_{\max}=\left\lfloor\frac{100}{36}\right\rfloor=2,$

so the contribution is

$(9+36+4)\frac{2\cdot 3}{2}=49\cdot 3=147.$

The pair \((2,2)\) is excluded because it is not coprime, and for \(n=3\) even the smallest possible largest term is

$(3+1)^2\cdot 3^2=144>100.$

Therefore

$S(100)=2925+147=3072,$

matching the checkpoint used by the implementation.

How the Code Works

The C++, Python, and Java implementations all use the parameterization above instead of testing arbitrary pairs \((a,b)\). They iterate the larger coprime parameter upward, stop as soon as \((n+1)^2n^2>N\), and for each admissible value scan the smaller parameter up to \(n\).

Non-coprime pairs are skipped immediately. For each remaining pair, the implementation builds the primitive triple \((a_0,b_0,c_0)\), breaks the inner loop when \(b_0>N\), computes

$t_{\max}=\left\lfloor\frac{N}{b_0}\right\rfloor,$

and adds

$(a_0+b_0+c_0)\frac{t_{\max}(t_{\max}+1)}{2}.$

So the code never searches invalid triples and never performs an expensive global brute-force scan. It sums complete scaling families directly.

Complexity Analysis

Let \(R=\lfloor N^{1/4}\rfloor\). Because the search only considers \(1\le m\le n\le R\), the number of candidate pairs is at most \(R(R+1)/2=O(\sqrt{N})\). Each pair needs one coprimality test and a constant amount of integer arithmetic, so the overall running time is \(O(\sqrt{N})\) in arithmetic operations, with \(O(1)\) extra memory.

Footnotes and References

  1. Problem page: https://projecteuler.net/problem=510
  2. Greatest common divisor: Wikipedia — Greatest common divisor
  3. Coprime integers: Wikipedia — Coprime integers
  4. Square number: Wikipedia — Square number
  5. Triangular number: Wikipedia — Triangular number

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

Previous: Problem 509 · All Project Euler solutions · Next: Problem 511

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! 🧮