Open Source · 7 Languages · 900+ Problems

Project Euler Solutions

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

All Problems

Problem 257: Angular Bisectors

View on Project Euler

Project Euler Problem 257 Solution

EulerSolve provides an optimized solution for Project Euler Problem 257, Angular Bisectors, with C++, Python, Java, and a step-by-step mathematical explanation.

Problem Summary Let \(a\le b\le c\) be the side lengths of a nondegenerate integer triangle \(ABC\), so its perimeter is $p=a+b+c.$ As in the problem statement, the angle bisectors cut the sides at points \(E,F,G\), and we are interested in the area ratio $\frac{[ABC]}{[AEG]}.$ We must count all integer-sided triangles with $a+b+c\le P$ for which that ratio is an integer. The final count for the full Euler limit is intentionally omitted here. Mathematical Approach 1. Converting the Geometry into an Arithmetic Ratio The code does not work directly with angles or areas. It first turns the geometry into a pure arithmetic condition. Let \(E\) be the point where the bisector at \(A\) meets \(BC\), and let \(G\) be the point where the bisector at \(B\) meets \(AC\). By the angle bisector theorem, $\frac{BE}{EC}=\frac{AB}{AC}=\frac{c}{b},\qquad \frac{AG}{GC}=\frac{AB}{BC}=\frac{c}{a}.$ Because \(BC=a\) and \(AC=b\), this gives $EC=\frac{ab}{b+c},\qquad AG=\frac{bc}{a+c}.$ Now compare areas in two stages....

Detailed mathematical approach

Problem Summary

Let \(a\le b\le c\) be the side lengths of a nondegenerate integer triangle \(ABC\), so its perimeter is

$p=a+b+c.$

As in the problem statement, the angle bisectors cut the sides at points \(E,F,G\), and we are interested in the area ratio

$\frac{[ABC]}{[AEG]}.$

We must count all integer-sided triangles with

$a+b+c\le P$

for which that ratio is an integer. The final count for the full Euler limit is intentionally omitted here.

Mathematical Approach

1. Converting the Geometry into an Arithmetic Ratio

The code does not work directly with angles or areas. It first turns the geometry into a pure arithmetic condition.

Let \(E\) be the point where the bisector at \(A\) meets \(BC\), and let \(G\) be the point where the bisector at \(B\) meets \(AC\). By the angle bisector theorem,

$\frac{BE}{EC}=\frac{AB}{AC}=\frac{c}{b},\qquad \frac{AG}{GC}=\frac{AB}{BC}=\frac{c}{a}.$

Because \(BC=a\) and \(AC=b\), this gives

$EC=\frac{ab}{b+c},\qquad AG=\frac{bc}{a+c}.$

Now compare areas in two stages. Triangles \(ABC\) and \(AEC\) share the altitude from \(A\) to line \(BC\), so

$\frac{[ABC]}{[AEC]}=\frac{BC}{EC}=\frac{a}{ab/(b+c)}=\frac{b+c}{b}.$

Likewise, triangles \(AEC\) and \(AEG\) share the altitude from \(E\) to line \(AC\), so

$\frac{[AEC]}{[AEG]}=\frac{AC}{AG}=\frac{b}{bc/(a+c)}=\frac{a+c}{c}.$

Multiplying the two ratios yields the key identity used everywhere in the solver:

$\frac{[ABC]}{[AEG]}=\frac{(a+b)(a+c)}{bc}.$

So the whole problem is equivalent to counting triangles for which

$k=\frac{(a+b)(a+c)}{bc}$

is an integer.

2. Why Only \(k=2,3,4\) Can Occur

Rewrite \(k\) as

$k=\left(1+\frac{a}{b}\right)\left(1+\frac{a}{c}\right).$

Because \(a\le b\le c\), we have

$0<\frac{a}{c}\le \frac{a}{b}\le 1.$

Therefore

$1<k\le 4.$

Since \(k\) must be an integer, the only possibilities are

$k\in\{2,3,4\}.$

This is the decisive simplification behind the code: instead of searching over all possible ratios, we only need to classify three arithmetic cases.

3. The Case \(k=4\): Exactly the Equilateral Triangles

To make

$\left(1+\frac{a}{b}\right)\left(1+\frac{a}{c}\right)=4,$

both factors must equal \(2\), because each factor is at most \(2\). That forces

$a=b=c.$

Hence the \(k=4\) contribution is simply the number of equilateral triangles with perimeter at most \(P\):

$\left\lfloor\frac{P}{3}\right\rfloor.$

4. The Case \(k=2\): A Product Equation

Starting from

$\frac{(a+b)(a+c)}{bc}=2,$

we get

$a^2+a(b+c)=bc.$

Rearranging gives

$bc-a(b+c)+a^2=2a^2,$

hence

$(b-a)(c-a)=2a^2.$

Now define

$d_1=b-a,\qquad d_2=c-a.$

Then \(d_1,d_2>0\) and

$d_1d_2=2a^2.$

Let

$g=\gcd(d_1,d_2),\qquad d_1=gu,\qquad d_2=gv,\qquad \gcd(u,v)=1.$

Because \(g\mid a\), write \(a=gh\). Then

$uv=2h^2.$

Since \(u\) and \(v\) are coprime and their product is twice a square, one of them must be a square and the other must be twice a square. So, after possibly swapping the two factors, there are exactly two canonical possibilities:

$u=x^2,\quad v=2y^2,$

or

$u=2x^2,\quad v=y^2,$

with

$\gcd(x,y)=1.$

5. The Two \(k=2\) Families

Family A. Take

$d_1=gx^2,\qquad d_2=2gy^2.$

Then \(a^2=d_1d_2/2=g^2x^2y^2\), so

$a=gxy.$

Therefore

$b=a+d_1=gx(x+y),$

$c=a+d_2=gy(x+2y).$

The perimeter becomes

$p=g(x+y)(x+2y).$

The ordering and triangle inequalities give the parameter range

$y<x\le \sqrt{2}\,y.$

Also \(x\) must be odd, because \(u=x^2\) must stay coprime to \(v=2y^2\).

For every primitive pair \((x,y)\), all positive scales

$1\le g\le \left\lfloor\frac{P}{(x+y)(x+2y)}\right\rfloor$

produce valid triangles.

Family B. Take

$d_1=2gx^2,\qquad d_2=gy^2.$

Then again

$a=gxy,$

and now

$b=a+d_1=gx(2x+y),$

$c=a+d_2=gy(x+y).$

The perimeter is

$p=g(x+y)(2x+y).$

The constraints become

$\sqrt{2}\,x\le y<2x.$

Here \(y\) must be odd so that \(y^2\) remains coprime to \(2x^2\). The number of valid scales is

$\left\lfloor\frac{P}{(x+y)(2x+y)}\right\rfloor.$

6. Worked Example for \(k=2\)

The smallest \(k=2\) triangle already appears in Family B. Take

$x=2,\qquad y=3,\qquad g=1.$

Then

$a=gxy=6,$

$b=gx(2x+y)=2(4+3)=14,$

$c=gy(x+y)=3(5)=15.$

So we get the triangle

$ (a,b,c)=(6,14,15),\qquad p=35. $

Checking the ratio:

$\frac{(a+b)(a+c)}{bc}=\frac{20\cdot 21}{14\cdot 15}=2.$

7. The Case \(k=3\): Another Product Equation

Starting from

$\frac{(a+b)(a+c)}{bc}=3,$

we obtain

$a^2+a(b+c)=2bc.$

From this, one convenient factorization is

$(2b-a)(2c-a)=3a^2.$

Now define

$u=2b-a,\qquad v=2c-a.$

Then

$uv=3a^2.$

Let

$u=gs,\qquad v=gt,\qquad \gcd(s,t)=1,\qquad a=gh.$

Then

$st=3h^2.$

Exactly the same squarefree reasoning shows that one factor must be a square and the other three times a square. So there are again two canonical forms:

$s=x^2,\quad t=3y^2,$

or

$s=3x^2,\quad t=y^2,$

with \(\gcd(x,y)=1\).

8. The Two \(k=3\) Families

Family A. Take

$u=gx^2,\qquad v=3gy^2.$

Then

$a=gxy,$

$b=\frac{u+a}{2}=\frac{gx(x+y)}{2},$

$c=\frac{v+a}{2}=\frac{gy(x+3y)}{2}.$

The perimeter is

$p=\frac{g(x+y)(x+3y)}{2}.$

The ordering and triangle inequalities become

$y<x\le \sqrt{3}\,y.$

Also \(x\not\equiv 0\pmod 3\), otherwise \(x^2\) would not stay coprime to \(3y^2\).

Because the formulas for \(b\) and \(c\) contain division by \(2\), parity matters. If \(x\) and \(y\) are both odd, then every positive \(g\) works. If one of them is even, then only even \(g\) produce integer side lengths.

Define

$B_A=(x+y)(x+3y),\qquad g_{\max}=\left\lfloor\frac{2P}{B_A}\right\rfloor.$

The contribution is

$g_{\max}$

when \(x,y\) are both odd, and

$\left\lfloor\frac{g_{\max}}{2}\right\rfloor$

otherwise.

Family B. Take

$u=3gx^2,\qquad v=gy^2.$

Then

$a=gxy,$

$b=\frac{u+a}{2}=\frac{gx(3x+y)}{2},$

$c=\frac{v+a}{2}=\frac{gy(x+y)}{2}.$

The perimeter is

$p=\frac{g(x+y)(3x+y)}{2}.$

Now the admissible range is

$\sqrt{3}\,x\le y<3x,$

and \(y\not\equiv 0\pmod 3\).

The parity rule is the same as in Family A: all \(g\) work when \(x,y\) are both odd, otherwise only even \(g\) work. If

$B_B=(x+y)(3x+y),\qquad g_{\max}=\left\lfloor\frac{2P}{B_B}\right\rfloor,$

then the contribution is \(g_{\max}\) or \(\lfloor g_{\max}/2\rfloor\) according to that parity test.

9. Worked Example for \(k=3\)

The smallest \(k=3\) triangle is the familiar

$ (4,5,6). $

It comes from Family B with

$x=1,\qquad y=2,\qquad g=2.$

Indeed,

$a=gxy=4,$

$b=\frac{gx(3x+y)}{2}=\frac{2\cdot 1\cdot 5}{2}=5,$

$c=\frac{gy(x+y)}{2}=\frac{2\cdot 2\cdot 3}{2}=6.$

And the ratio is

$\frac{(a+b)(a+c)}{bc}=\frac{9\cdot 10}{5\cdot 6}=3.$

This example also illustrates the parity rule: with \(x=1\) and \(y=2\), odd \(g\) would make \(b\) and \(c\) half-integers, so the first legal choice is \(g=2\).

10. Why the Count Has No Double Counting

Every valid triangle belongs to exactly one integer ratio \(k\in\{2,3,4\}\).

The case \(k=4\) is equilateral and is completely separate.

For \(k=2\), once \(d_1=b-a\) and \(d_2=c-a\) are normalized by their gcd, the coprime product \(uv=2h^2\) has a unique squarefree split: the factor \(2\) lies either in the first normalized part or in the second. That is exactly the A/B split above. The same uniqueness argument holds for \(k=3\), with the factor \(3\) lying in one coprime part or the other.

Finally, for fixed primitive parameters \((x,y)\), changing \(g\) only scales the triangle. So every valid triangle is generated exactly once.

11. Validation Checkpoints

Let \(N(P)\) denote the number of valid triangles with perimeter at most \(P\). The code contains a brute-force verifier and checks the fast formulas against it for

$P\in\{40,60,100,150,200,300\}.$

Those brute-force totals are

$N(40)=16,\quad N(60)=26,\quad N(100)=46,$

$N(150)=72,\quad N(200)=100,\quad N(300)=160.$

For example, \(N(40)=16\) breaks into 13 equilateral triangles, two \(k=3\) triangles \((4,5,6)\) and \((8,10,12)\), and one \(k=2\) triangle \((6,14,15)\).

How the Code Works

The function count_valid_triangles(P) starts with the equilateral contribution \(\lfloor P/3\rfloor\), sets

$\text{max\_var}=\lfloor\sqrt{P}\rfloor+2,$

and then counts the four nontrivial families. Each family loops over primitive parameter pairs \((x,y)\), applies the coprimality, parity, and mod-3 filters derived above, computes the base perimeter expression, and adds the number of admissible scale factors \(g\).

The four family counters are independent, so the implementation can distribute them across up to four pthread workers. If thread creation fails, it falls back to the serial path. The helper brute_count is used only for checkpoint verification on small perimeter limits.

Complexity Analysis

The parameters \(x\) and \(y\) only range up to \(O(\sqrt{P})\), so the search is roughly quadratic in \(\sqrt{P}\), that is, about \(O(P)\) candidate pairs. Each candidate only needs a gcd computation and a constant amount of integer arithmetic. Thus the solution is fast enough for \(P=10^8\) while using only constant extra memory apart from tiny thread-local contexts.

Footnotes and References

  1. Problem page: https://projecteuler.net/problem=257
  2. Angle bisector theorem: Wikipedia - Angle bisector theorem
  3. Diophantine equations: Wikipedia - Diophantine equation
  4. Coprime integers: Wikipedia - Coprime integers
  5. Floor and ceiling functions: Wikipedia - Floor and ceiling functions

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

Previous: Problem 256 · All Project Euler solutions · Next: Problem 258

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