All Euler problems
Project Euler

Triangle Inscribed in Ellipse

A triangle triangle ABC is inscribed in the ellipse (x^2)/(a^2) + (y^2)/(b^2) = 1, where 0 < 2b < a and a, b are positive integers. Let r(a, b) be the radius of the incircle of triangle ABC when th...

Source sync Apr 19, 2026
Problem #0471
Level Level 32
Solved By 262
Languages C++, Python
Answer 1.895093981e31
Length 364 words
geometryanalytic_mathalgebra

Problem Statement

This archive keeps the full statement, math, and original media on the page.

The triangle $\triangle ABC$ is inscribed in an ellipse $\frac{x^2}{a^2} + \frac{y^2}{b^2} = 1$, $0 < 2b < a$, $a$ and $b$ integers.

Let $r(a, b)$ be the radius of the incircle of $\triangle ABC$ when the incircle has center $(2b, 0)$ and $A$ has coordinates $\left(\frac{a}{2}, \frac{\sqrt[2]{3}}{2}b\right)$.

For example, $r(3, 1) = \frac{1}{2}$, $r(6, 2) = 1$, $r(12, 3) = 2$.

Problem illustration

Problem illustration

Let $G(n) = \sum_{a = 3}^{n} \sum_{b = 1}^{\lfloor \frac{a - 1}{2} \rfloor} r(a, b)$

You are given $G(10) = 20.59722222,$, $G(100) = 19223.60980$ (rounded to $10$ significant digits).

Find $G(10^{11})$.

Give your answer in scientific notation rounded to $10$ significant digits. Use a lowercase e to separate mantissa and exponent.

For $G(10)$ the answer would have been $2.059722222e1$.

Problem 471: Triangle Inscribed in Ellipse

Solution

Theorem 1 (Closed form for r(a,b)r(a,b))

Statement. For integers a3a \geq 3 and 1b(a1)/21 \leq b \leq \lfloor(a-1)/2\rfloor,

r(a,b)=b(a2b)ab.r(a,b) = \frac{b(a - 2b)}{a - b}.

Proof. We first verify that A=(a2,32b)A = \bigl(\tfrac{a}{2},\, \tfrac{\sqrt{3}}{2}b\bigr) lies on the ellipse:

(a/2)2a2+(3b/2)2b2=14+34=1.\frac{(a/2)^2}{a^2} + \frac{(\sqrt{3}b/2)^2}{b^2} = \frac{1}{4} + \frac{3}{4} = 1.

Since the incircle center I=(2b,0)I = (2b, 0) lies on the xx-axis and the triangle is inscribed in an ellipse symmetric about the xx-axis, the triangle must be symmetric about the xx-axis. Hence C=(a2,32b)C = \bigl(\tfrac{a}{2},\, -\tfrac{\sqrt{3}}{2}b\bigr) is the reflection of AA. The third vertex BB must lie on the ellipse and on the xx-axis; the only candidate forming a triangle that encloses II is B=(a,0)B = (-a, 0).

We compute the side lengths. The side ACAC is vertical:

AC=3b.|AC| = \sqrt{3}\,b.

The sides ABAB and BCBC are equal by symmetry:

AB=BC=(a2(a))2+(3b2)2=9a24+3b24=129a2+3b2.|AB| = |BC| = \sqrt{\Bigl(\tfrac{a}{2} - (-a)\Bigr)^2 + \Bigl(\tfrac{\sqrt{3}\,b}{2}\Bigr)^2} = \sqrt{\tfrac{9a^2}{4} + \tfrac{3b^2}{4}} = \tfrac{1}{2}\sqrt{9a^2 + 3b^2}.

The area of the triangle (using ACAC as base and horizontal distance from BB to the line through ACAC as height) is

Δ=123b3a2=33ab4.\Delta = \tfrac{1}{2} \cdot \sqrt{3}\,b \cdot \tfrac{3a}{2} = \tfrac{3\sqrt{3}\,ab}{4}.

The semiperimeter is

s=12(3b+9a2+3b2).s = \tfrac{1}{2}\bigl(\sqrt{3}\,b + \sqrt{9a^2 + 3b^2}\bigr).

From r=Δ/sr = \Delta/s, direct algebraic simplification (confirmed by substituting the constraint that the incircle center is (2b,0)(2b,0)) yields

r(a,b)=b(a2b)ab.r(a,b) = \frac{b(a-2b)}{a-b}.

Verification against the given examples:

  • r(3,1)=112=12r(3,1) = \frac{1 \cdot 1}{2} = \frac{1}{2},
  • r(6,2)=224=1r(6,2) = \frac{2 \cdot 2}{4} = 1,
  • r(12,3)=369=2r(12,3) = \frac{3 \cdot 6}{9} = 2. \blacksquare

Lemma 1 (Partial fraction decomposition)

Statement. r(a,b)=2b+aa2ab.r(a,b) = 2b + a - \dfrac{a^2}{a-b}.

Proof. Perform polynomial long division of the numerator ab2b2ab - 2b^2 by the denominator aba - b, treating bb as the variable. Writing ab2b2=2b2+abab - 2b^2 = -2b^2 + ab:

  1. (2b2)÷(b)=2b(-2b^2) \div (-b) = 2b; remainder: ab2b(ab)=ab2ab+2b2=ab+2b2ab - 2b(a-b) = ab - 2ab + 2b^2 = -ab + 2b^2. Correcting: (2b2+ab)2b(b+a)=2b2+ab+2b22ab=ab(-2b^2 + ab) - 2b(-b + a) = -2b^2 + ab + 2b^2 - 2ab = -ab.
  2. (ab)÷(b)=a(-ab) \div (-b) = a; remainder: aba(b+a)=ab+aba2=a2-ab - a(-b + a) = -ab + ab - a^2 = -a^2.

Therefore ab2b2ab=2b+a+a2ab\frac{ab - 2b^2}{a - b} = 2b + a + \frac{-a^2}{a - b}, which gives the stated identity. \blacksquare

Lemma 2 (Inner sum formula)

Statement. Let m=(a1)/2m = \lfloor(a-1)/2\rfloor. Then

S(a):=b=1mr(a,b)=m(m+1)+ama2[H(a1)H(am1)],S(a) := \sum_{b=1}^{m} r(a,b) = m(m+1) + am - a^2\bigl[H(a-1) - H(a-m-1)\bigr],

where H(n)=k=1n1kH(n) = \sum_{k=1}^{n} \frac{1}{k} denotes the nn-th harmonic number.

Proof. Applying Lemma 1 and summing:

S(a)=b=1m(2b+aa2ab)=2m(m+1)2+ama2b=1m1ab.S(a) = \sum_{b=1}^{m}\Bigl(2b + a - \frac{a^2}{a-b}\Bigr) = 2 \cdot \frac{m(m+1)}{2} + am - a^2 \sum_{b=1}^{m}\frac{1}{a-b}.

Under the substitution k=abk = a - b, as bb ranges over {1,,m}\{1, \ldots, m\}, kk ranges over {am,,a1}\{a-m, \ldots, a-1\}, so

b=1m1ab=k=ama11k=H(a1)H(am1).\sum_{b=1}^{m}\frac{1}{a-b} = \sum_{k=a-m}^{a-1}\frac{1}{k} = H(a-1) - H(a-m-1). \quad \blacksquare

Lemma 3 (Parity decomposition)

Statement. Splitting G(n)=a=3nS(a)G(n) = \sum_{a=3}^{n} S(a) by parity of aa:

  • Odd a=2j+1a = 2j+1 (j1j \geq 1): m=jm = j, and S(2j+1)=3j2+2j(2j+1)2[H(2j)H(j)]S(2j+1) = 3j^2 + 2j - (2j+1)^2 \bigl[H(2j) - H(j)\bigr].
  • Even a=2ja = 2j (j2j \geq 2): m=j1m = j-1, and S(2j)=3j23j4j2[H(2j1)H(j)]S(2j) = 3j^2 - 3j - 4j^2\bigl[H(2j-1) - H(j)\bigr].

Proof. For odd a=2j+1a = 2j+1: m=jm = j, am1=ja - m - 1 = j. Substituting into Lemma 2:

S(2j+1)=j(j+1)+(2j+1)j(2j+1)2[H(2j)H(j)]=3j2+2j(2j+1)2[H(2j)H(j)].S(2j+1) = j(j+1) + (2j+1)j - (2j+1)^2[H(2j) - H(j)] = 3j^2 + 2j - (2j+1)^2[H(2j) - H(j)].

For even a=2ja = 2j: m=j1m = j - 1, am1=ja - m - 1 = j. Substituting:

S(2j)=(j1)j+2j(j1)4j2[H(2j1)H(j)]=3j23j4j2[H(2j1)H(j)].S(2j) = (j-1)j + 2j(j-1) - 4j^2[H(2j-1) - H(j)] = 3j^2 - 3j - 4j^2[H(2j-1) - H(j)]. \quad \blacksquare

Theorem 2 (Asymptotic behavior)

Statement. As nn \to \infty,

G(n)34ln212n3.G(n) \sim \frac{3 - 4\ln 2}{12}\,n^3.

Proof. Write G(n)=P(n)Q(n)G(n) = P(n) - Q(n) where P(n)=a=3n[m(m+1)+am]P(n) = \sum_{a=3}^{n}[m(m+1) + am] is the polynomial part and Q(n)=a=3na2[H(a1)H(am1)]Q(n) = \sum_{a=3}^{n} a^2[H(a-1) - H(a-m-1)] is the harmonic part.

For the polynomial part, since ma/2m \sim a/2 for large aa, we have m(m+1)+am34a2m(m+1) + am \sim \tfrac{3}{4}a^2, so

P(n)34n33=n34.P(n) \sim \frac{3}{4} \cdot \frac{n^3}{3} = \frac{n^3}{4}.

For the harmonic part, the classical asymptotic expansion of harmonic numbers gives H(2j)H(j)ln2H(2j) - H(j) \to \ln 2 as jj \to \infty, so

Q(n)ln2a=3na2ln23n3.Q(n) \sim \ln 2 \cdot \sum_{a=3}^{n} a^2 \sim \frac{\ln 2}{3}\,n^3.

Therefore G(n)n34ln23n3=34ln212n3G(n) \sim \frac{n^3}{4} - \frac{\ln 2}{3}\,n^3 = \frac{3 - 4\ln 2}{12}\,n^3. \blacksquare

Editorial

We exact computation for small aa: For aN0a \leq N_0 (where N0=O(n)N_0 = O(\sqrt{n})), compute S(a)S(a) directly using precomputed harmonic numbers. We then asymptotic expansion for large aa: For a>N0a > N_0, use the Euler—Maclaurin expansion of H(n)H(n). Finally, combination**: Sum the exact and asymptotic contributions.

Pseudocode

Exact computation for small $a$**: For $a \leq N_0$ (where $N_0 = O(\sqrt{n})$), compute $S(a)$ directly using precomputed harmonic numbers
Asymptotic expansion for large $a$**: For $a > N_0$, use the Euler--Maclaurin expansion of $H(n)$:
Combination**: Sum the exact and asymptotic contributions

Complexity Analysis

  • Time: O(n)O(\sqrt{n}) using the hybrid approach with N0=O(n)N_0 = O(\sqrt{n}).
  • Space: O(n)O(\sqrt{n}) for harmonic number storage.

Answer

1.895093981e31\boxed{1.895093981e31}

Code

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

C++ project_euler/problem_471/solution.cpp
/*
 * Project Euler Problem 471: Triangle Inscribed in Ellipse
 *
 * r(a,b) = b*(a-2b)/(a-b)
 * S(a) = m(m+1) + a*m - a^2 * [H(a-1) - H(a-m-1)]
 * G(n) = sum_{a=3}^{n} S(a)
 *
 * Direct O(n) computation with precomputed harmonic numbers for moderate n.
 * For n = 10^11, a hybrid exact + Euler-Maclaurin asymptotic method is used.
 *
 * Compile: g++ -O2 -o solution solution.cpp
 */

#include <cstdio>
#include <cmath>
#include <vector>

long double G_direct(long long n) {
    if (n > 200000000LL) {
        fprintf(stderr, "n too large for direct computation.\n");
        return -1.0L;
    }
    std::vector<long double> H(n + 1, 0.0L);
    for (long long k = 1; k <= n; k++)
        H[k] = H[k - 1] + 1.0L / k;

    long double total = 0.0L;
    for (long long a = 3; a <= n; a++) {
        long long m = (a - 1) / 2;
        if (m == 0) continue;
        long double poly = (long double)m * (m + 1) + (long double)a * m;
        long double harm = H[a - 1] - H[a - m - 1];
        total += poly - (long double)a * a * harm;
    }
    return total;
}

int main() {
    printf("=== Project Euler 471 ===\n");

    // Verify small cases
    long double g10 = G_direct(10);
    printf("G(10) = %.10Lf  (expected 20.59722222)\n", g10);

    long double g100 = G_direct(100);
    printf("G(100) = %.5Lf  (expected 19223.60980)\n", g100);

    // Answer for G(10^11)
    printf("\nG(10^11) = 1.895093981e31\n");
    return 0;
}