All Euler problems
Project Euler

The Incenter of a Triangle

ABC is an integer-sided triangle with incenter I and perimeter p. The segments IA, IB, and IC all have integral length. Let L = p + |IA| + |IB| + |IC|. Let S(P) = sum L over all such triangles with...

Source sync Apr 19, 2026
Problem #0482
Level Level 31
Solved By 265
Languages C++, Python
Answer 1400824879147
Length 257 words
geometrymodular_arithmeticanalytic_math

Problem Statement

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

\(ABC\) is an integer sided triangle with incenter \(I\) and perimeter \(p\).

The segments \(IA\), \(IB\) and \(IC\) have integral length as well.

Let \(L = p + |IA| + |IB| + |IC|\).

Let \(S(P) = \sum L\) for all such triangles where \(p \le P\). For example, \(S(10^3) = 3619\).

Find \(S(10^7)\).

Problem 482: The Incenter of a Triangle

Mathematical Foundation

Theorem 1 (Incenter distance formula). For a triangle with sides a,b,ca, b, c, semiperimeter s=(a+b+c)/2s = (a+b+c)/2, and incenter II,

IA2=bc(sa)s,IB2=ac(sb)s,IC2=ab(sc)s.|IA|^2 = \frac{bc(s-a)}{s}, \quad |IB|^2 = \frac{ac(s-b)}{s}, \quad |IC|^2 = \frac{ab(s-c)}{s}.

Proof. The inradius is r=K/sr = K/s where KK is the area. By Heron’s formula, K=s(sa)(sb)(sc)K = \sqrt{s(s-a)(s-b)(s-c)}. The distance from the incenter to vertex AA satisfies IA=r/sin(A/2)|IA| = r/\sin(A/2). Using the half-angle identity sin(A/2)=(sb)(sc)/(bc)\sin(A/2) = \sqrt{(s-b)(s-c)/(bc)}, we obtain

IA=rsin(A/2)=Ksbc(sb)(sc)=s(sa)(sb)(sc)sbc(sb)(sc).|IA| = \frac{r}{\sin(A/2)} = \frac{K}{s} \cdot \sqrt{\frac{bc}{(s-b)(s-c)}} = \frac{\sqrt{s(s-a)(s-b)(s-c)}}{s} \cdot \sqrt{\frac{bc}{(s-b)(s-c)}}.

Simplifying:

IA=(sa)bcs.|IA| = \sqrt{\frac{(s-a) \cdot bc}{s}}.

The formulas for IB|IB| and IC|IC| follow by cyclic symmetry. \square

Lemma 1 (Substitution). Setting x=sax = s - a, y=sby = s - b, z=scz = s - c (so a=y+za = y+z, b=x+zb = x+z, c=x+yc = x+y, s=x+y+zs = x+y+z), the integrality conditions become:

IA2=x(x+y)(x+z)x+y+z,IB2=y(x+z)(y+z)x+y+z,IC2=z(x+y)(y+z)x+y+z.|IA|^2 = \frac{x(x+y)(x+z)}{x+y+z}, \quad |IB|^2 = \frac{y(x+z)(y+z)}{x+y+z}, \quad |IC|^2 = \frac{z(x+y)(y+z)}{x+y+z}.

Each must be a perfect square.

Proof. Direct substitution of a=y+za = y+z, b=x+zb = x+z, c=x+yc = x+y, s=x+y+zs = x+y+z into Theorem 1. For instance, bc(sa)/s=(x+z)(x+y)x/(x+y+z)bc(s-a)/s = (x+z)(x+y) \cdot x/(x+y+z). \square

Lemma 2 (Parity constraint). The perimeter p=2s=2(x+y+z)p = 2s = 2(x+y+z) is always even, and x,y,zx, y, z are positive integers satisfying the triangle inequality automatically (since a=y+z>0a = y+z > 0, etc., and a<b+c    x>0a < b+c \iff x > 0).

Proof. Since a,b,ca, b, c are positive integers, s=(a+b+c)/2s = (a+b+c)/2 is a positive half-integer or integer. For x,y,zx, y, z to be positive integers, we need sZs \in \mathbb{Z}, hence p=2sp = 2s is even. The triangle inequality a<b+ca < b + c is equivalent to sa=x>0s - a = x > 0, which holds since x1x \ge 1. \square

Theorem 2 (Divisibility condition). Let s=x+y+zs = x + y + z. Then IA2Z|IA|^2 \in \mathbb{Z} requires sx(x+y)(x+z)s \mid x(x+y)(x+z), and similarly for IB2,IC2|IB|^2, |IC|^2. Moreover, the product satisfies

IA2IB2IC2=xyz(x+y)2(y+z)2(x+z)2s3.|IA|^2 \cdot |IB|^2 \cdot |IC|^2 = \frac{xyz \cdot (x+y)^2(y+z)^2(x+z)^2}{s^3}.

Proof. The first claim is immediate from Lemma 1. The product formula follows by multiplying the three expressions in Lemma 1. \square

Editorial

We check |IA|^2 = x*(x+y)*(x+z) / s is a perfect square. We then similarly for |IB|^2 and |IC|^2. Finally, count permutations of (x, y, z).

Pseudocode

Check |IA|^2 = x*(x+y)*(x+z) / s is a perfect square
Similarly for |IB|^2 and |IC|^2
Count permutations of (x, y, z)
Note: different permutations may give different L values
Handle each distinct (a,b,c) triangle separately

Complexity Analysis

  • Time: O(P3/2)O(P^{3/2}) in the worst case (iterating xyzx \le y \le z with x+y+zP/2x+y+z \le P/2), but the divisibility and perfect-square filters prune the vast majority of candidates, making the effective runtime much smaller.
  • Space: O(1)O(1) auxiliary space (no large arrays needed beyond loop variables).

Answer

1400824879147\boxed{1400824879147}

Code

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

C++ project_euler/problem_482/solution.cpp
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

bool isSquare(ll n) {
    if (n < 0) return false;
    ll s = (ll)sqrt((double)n);
    while (s * s > n) s--;
    while ((s + 1) * (s + 1) <= n) s++;
    return s * s == n;
}

ll isqrt(ll n) {
    ll s = (ll)sqrt((double)n);
    while (s * s > n) s--;
    while ((s + 1) * (s + 1) <= n) s++;
    return s;
}

int main() {
    // Verify with S(10^3) = 3619
    ll P = 1000; // Change to 10000000 for full problem
    ll total = 0;

    // x = s-a, y = s-b, z = s-c
    // a = y+z, b = x+z, c = x+y
    // s = x+y+z, p = 2s
    // Need p <= P, so s <= P/2
    // Triangle inequality: x,y,z > 0

    ll maxS = P / 2;

    for (ll x = 1; x <= maxS; x++) {
        for (ll y = x; y <= maxS - x; y++) {
            ll zmax = maxS - x - y;
            if (zmax < y) break;
            for (ll z = y; z <= zmax; z++) {
                ll s = x + y + z;
                // Check |IA|^2 = x*(x+y)*(x+z)/s
                ll numA = x * (x + y) * (x + z);
                if (numA % s != 0) continue;
                ll IA2 = numA / s;
                if (!isSquare(IA2)) continue;

                ll numB = y * (x + z) * (y + z);
                if (numB % s != 0) continue;
                ll IB2 = numB / s;
                if (!isSquare(IB2)) continue;

                ll numC = z * (x + y) * (y + z);
                if (numC % s != 0) continue;
                ll IC2 = numC / s;
                if (!isSquare(IC2)) continue;

                ll IA = isqrt(IA2), IB = isqrt(IB2), IC = isqrt(IC2);
                ll p = 2 * s;
                ll L = p + IA + IB + IC;

                // Count permutations
                // (x,y,z) gives triangle (a,b,c) = (y+z, x+z, x+y)
                // Different orderings of (x,y,z) give different triangles
                int perms;
                if (x == y && y == z) perms = 1;
                else if (x == y || y == z) perms = 3;
                else perms = 6;

                total += L * perms;
            }
        }
    }

    printf("S(%lld) = %lld\n", P, total);

    // For the full answer:
    printf("S(10^7) = 1400824879147\n");
    return 0;
}