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...
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 is equivalent to the existence of a positive integer (with ) such that
Proof. requires . Setting , this is , which rearranges to . Moreover must be even (since ).
Lemma 1 (Sum-Difference Substitution). Let and with , . Then the equation becomes
Proof. We have . Substituting: .
Theorem 2 (Two-Square Representation Identity). The equation (representing the same integer as a sum of two squares in two ways) can be parametrized using the Brahmagupta—Fibonacci identity via Gaussian integers:
Any two representations of arise from different factorizations of in .
Proof. This follows from the unique factorization in (a principal ideal domain) and the norm-multiplicativity where . Distinct representations correspond to grouping the Gaussian prime factors of differently between and .
Lemma 2 (Constraint Translation). The triangle inequality translates to . The ordering requires and .
Proof. Direct substitution: . Also and .
Editorial
Optimized approach:* For each , iterate over valid values and derive from the two-square decomposition, or iterate over with and check the perfect-square condition on for 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 , iterate over valid values and derive from the two-square decomposition, or iterate over with and check the perfect-square condition on for 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.
#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;
}
"""
Problem 513: Integral Median
A triangle with integer sides a <= b <= c has median to side a:
m_a = (1/2) * sqrt(2b^2 + 2c^2 - a^2)
Count triangles with a <= b <= c <= N where m_a is a positive integer.
"""
import math
def solve(N: int):
"""
Count triangles (a, b, c) with a <= b <= c <= N where
m_a = (1/2)*sqrt(2b^2 + 2c^2 - a^2) is a positive integer.
We need 2b^2 + 2c^2 - a^2 = 4*m^2 for some positive integer m.
Equivalently: 2b^2 + 2c^2 - a^2 must be a perfect square of an even number.
"""
count = 0
for b in range(1, N + 1):
for c in range(b, N + 1):
val_base = 2 * b * b + 2 * c * c
# a ranges from 1 to b, and triangle inequality: a + b > c => a > c - b
a_min = max(1, c - b + 1)
a_max = b
for a in range(a_min, a_max + 1):
val = val_base - a * a
if val <= 0:
continue
sq = math.isqrt(val)
if sq * sq == val and sq % 2 == 0:
count += 1
return count
def solve_brute_force(N: int):
"""Direct brute-force for small N verification."""
count = 0
for a in range(1, N + 1):
for b in range(a, N + 1):
for c in range(b, N + 1):
if a + b <= c:
continue
val = 2 * b * b + 2 * c * c - a * a
if val > 0:
sq = math.isqrt(val)
if sq * sq == val and sq % 2 == 0:
count += 1
return count
# Verify on small inputs
N_small = 50
ans1 = solve(N_small)
ans2 = solve_brute_force(N_small)
assert ans1 == ans2, f"Mismatch: {ans1} vs {ans2}"
print(f"T({N_small}) = {ans1}")
# Show growth for moderate N values
Ns = list(range(10, 201, 10))
counts = [solve(n) for n in Ns]
for n, c in zip(Ns, counts):
print(f"T({n}) = {c}")