All Euler problems
Project Euler

Integral Median

A triangle has integer sides a <= b <= c. The median to side a has length m_a = (1)/(2)sqrt(2b^2 + 2c^2 - a^2). Let T(N) count the number of such triangles with a <= b <= c <= N for which m_a is a...

Source sync Apr 19, 2026
Problem #0513
Level Level 27
Solved By 363
Languages C++, Python
Answer 2925619196
Length 259 words
geometryanalytic_mathmodular_arithmetic

Problem Statement

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

\(ABC\) is an integral sided triangle with sides \(a \le b \le c\).

\(m_C\) is the median connecting \(C\) and the midpoint of \(AB\).

\(F(n)\) is the number of such triangles with \(c \le n\) for which \(m_C\) has integral length as well.

\(F(10)=3\) and \(F(50)=165\).

Find \(F(100000)\).

Problem 513: Integral Median

Mathematical Foundation

Theorem 1 (Median Diophantine Reduction). The condition maZ+m_a \in \mathbb{Z}^+ is equivalent to the existence of a positive integer dd (with d=2mad = 2m_a) such that

a2+d2=2(b2+c2).a^2 + d^2 = 2(b^2 + c^2).

Proof. maZ+m_a \in \mathbb{Z}^+ requires 2b2+2c2a2=(2ma)2>02b^2 + 2c^2 - a^2 = (2m_a)^2 > 0. Setting d=2mad = 2m_a, this is 2b2+2c2a2=d22b^2 + 2c^2 - a^2 = d^2, which rearranges to a2+d2=2(b2+c2)a^2 + d^2 = 2(b^2 + c^2). Moreover dd must be even (since d=2mad = 2m_a). \square

Lemma 1 (Sum-Difference Substitution). Let s=b+cs = b + c and t=cbt = c - b with 0tcb0 \leq t \leq c - b, st(mod2)s \equiv t \pmod{2}. Then the equation becomes

a2+d2=s2+t2.a^2 + d^2 = s^2 + t^2.

Proof. We have b2+c2=(st)2+(s+t)24=s2+t22b^2 + c^2 = \frac{(s-t)^2 + (s+t)^2}{4} = \frac{s^2 + t^2}{2}. Substituting: a2+d2=2s2+t22=s2+t2a^2 + d^2 = 2 \cdot \frac{s^2 + t^2}{2} = s^2 + t^2. \square

Theorem 2 (Two-Square Representation Identity). The equation a2+d2=s2+t2a^2 + d^2 = s^2 + t^2 (representing the same integer as a sum of two squares in two ways) can be parametrized using the Brahmagupta—Fibonacci identity via Gaussian integers:

(a+di)(adi)=(s+ti)(sti)in Z[i].(a + di)(a - di) = (s + ti)(s - ti) \quad \text{in } \mathbb{Z}[i].

Any two representations of n=a2+d2=s2+t2n = a^2 + d^2 = s^2 + t^2 arise from different factorizations of nn in Z[i]\mathbb{Z}[i].

Proof. This follows from the unique factorization in Z[i]\mathbb{Z}[i] (a principal ideal domain) and the norm-multiplicativity N(αβ)=N(α)N(β)N(\alpha\beta) = N(\alpha)N(\beta) where N(x+yi)=x2+y2N(x + yi) = x^2 + y^2. Distinct representations correspond to grouping the Gaussian prime factors of nn differently between α\alpha and αˉ\bar{\alpha}. \square

Lemma 2 (Constraint Translation). The triangle inequality a+b>ca + b > c translates to a>t=cba > t = c - b. The ordering abca \leq b \leq c requires ab=st2a \leq b = \frac{s-t}{2} and c=s+t2Nc = \frac{s+t}{2} \leq N.

Proof. Direct substitution: a+b>ca>cb=ta + b > c \Leftrightarrow a > c - b = t. Also ab=(st)/2a \leq b = (s-t)/2 and c=(s+t)/2Nc = (s+t)/2 \leq N. \square

Editorial

Optimized approach:* For each aa, iterate over valid mam_a values and derive (b,c)(b, c) from the two-square decomposition, or iterate over (b,c)(b, c) with bcNb \leq c \leq N and check the perfect-square condition on 2b2+2c2a22b^2 + 2c^2 - a^2 for aa in the valid range. We triangle inequality: a + b > c.

Pseudocode

    Set count <- 0
    For b from 1 to N:
        For c from b to N:
            Set val <- 2 * b^2 + 2 * c^2
            For a from 1 to b:
                Set r <- val - a^2
                If r > 0 and r mod 4 == 0 then
                    Set m <- isqrt(r)
                    If m * m == r then
                        triangle inequality: a + b > c
                        If a + b > c then
                            count += 1
    Return count

Optimized approach: For each aa, iterate over valid mam_a values and derive (b,c)(b, c) from the two-square decomposition, or iterate over (b,c)(b, c) with bcNb \leq c \leq N and check the perfect-square condition on 2b2+2c2a22b^2 + 2c^2 - a^2 for aa in the valid range.


## Complexity Analysis

- **Time:** $O(N^2)$ with the optimized enumeration over $(b, c)$ pairs and $O(1)$ perfect-square check per valid $a$.
- **Space:** $O(N)$ for auxiliary arrays (e.g., precomputed squares).

## Answer

$$
\boxed{2925619196}
$$

Code

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

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

int main() {
    // Count triangles (a <= b <= c <= N) with integer median m_a
    // m_a = (1/2)*sqrt(2b^2 + 2c^2 - a^2), need this to be a positive integer
    // => 2b^2 + 2c^2 - a^2 = (2m)^2 for some positive integer m

    long long N;
    cout << "Enter N: ";
    cin >> N;

    long long count = 0;
    for (long long b = 1; b <= N; b++) {
        for (long long c = b; c <= N; c++) {
            long long base = 2LL * b * b + 2LL * c * c;
            long long a_min = max(1LL, c - b + 1);
            long long a_max = b;
            for (long long a = a_min; a <= a_max; a++) {
                long long val = base - a * a;
                if (val <= 0) continue;
                long long sq = (long long)sqrt((double)val);
                // Adjust for floating-point errors
                while (sq * sq > val) sq--;
                while ((sq + 1) * (sq + 1) <= val) sq++;
                if (sq * sq == val && sq % 2 == 0) {
                    count++;
                }
            }
        }
    }

    cout << "T(" << N << ") = " << count << endl;
    return 0;
}