Investigating the Torricelli Point of a Triangle
Let ABC be a triangle with all interior angles less than 120 degrees. Let T be the Torricelli (Fermat) point of the triangle, and let p = |AT|, q = |BT|, r = |CT|. It can be shown that p, q, r sati...
Problem Statement
This archive keeps the full statement, math, and original media on the page.

Problem 143: Investigating the Torricelli Point of a Triangle
Mathematical Foundation
Theorem 1. At the Torricelli point T of a triangle with all angles less than , each pair of vertices subtends an angle of exactly at T. In particular, for side :
Proof. The Torricelli point minimizes and, when all angles are less than , lies in the interior with . Applying the law of cosines to triangle ATB:
The other two equations follow by cyclic symmetry.
Definition. A pair with is called 120-compatible if is a perfect square.
Theorem 2. All primitive 120-compatible pairs (with ) are parametrized by:
where , , and . The corresponding square root is .
Proof. We verify: .
Expanding each term:
Summing: .
For primitivity: if , then , which implies and , so , violating primitivity. The converse (that and imply ) can be verified by checking that any common prime factor of and must divide , and showing this leads to a contradiction using the Eisenstein integer norm.
Lemma 1. All 120-compatible pairs are of the form where is a primitive pair and .
Proof. If is 120-compatible with , then where , , . Since is a perfect square (being times a perfect square), is a primitive 120-compatible pair.
Lemma 2. A valid Torricelli triple corresponds to a 3-clique in the graph whose vertices are positive integers and whose edges connect all 120-compatible pairs.
Proof. The three Torricelli equations require , , and to each be 120-compatible. This is exactly the condition that forms a clique of size 3 in .
Editorial
At the Torricelli point T, angles ATB = BTC = CTA = 120 degrees. By cosine rule: a^2 = q^2 + qr + r^2, etc. We need p, q, r positive integers with p^2+pq+q^2, p^2+pr+r^2, q^2+qr+r^2 all perfect squares, and p+q+r <= 120000. 120-compatible pairs: (u,v) with u^2+uv+v^2 a perfect square. Primitive parametrization: u = m^2-n^2, v = 2mn+n^2 with m > n >= 1, gcd(m,n) = 1, (m-n) not divisible by 3. Then u^2+uv+v^2 = (m^2+mn+n^2)^2. All solutions are k*(u,v) for positive integer k. We generate all 120-compatible pairs. We then add both orderings. Finally, also consider (v0, u0) as a pair (swap roles).
Pseudocode
INPUT: L = 120000
OUTPUT: Sum of all distinct values p+q+r <= L
GENERATE ALL 120-COMPATIBLE PAIRS:
Add both orderings
Also consider (v0, u0) as a pair (swap roles)
Similarly for (v0, u0) primitive pair if v0 > u0
FIND ALL 3-CLIQUES:
for each vertex p in pairs
RETURN sum of all elements in sums
Complexity Analysis
- Time: Pair generation is where is the number of primitive pairs with parameters up to , which is by density estimates for Eisenstein primes. Clique enumeration depends on graph density; in practice, the neighbor sets are small enough that intersection is fast. Overall: in the worst case.
- Space: where is the number of 120-compatible pairs, for storing the adjacency lists.
Answer
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() {
// Problem 143: Torricelli point distances
// Find sum of all distinct p+q+r <= 120000
// where all three pair-wise expressions u^2+uv+v^2 are perfect squares.
//
// Primitive 120-compatible pairs: u = m^2-n^2, v = 2mn+n^2
// with m > n >= 1, gcd(m,n) = 1, (m-n) % 3 != 0.
// Then u^2+uv+v^2 = (m^2+mn+n^2)^2.
// All solutions are k*(u,v) for k >= 1.
const int LIMIT = 120000;
// smaller[u] = sorted vector of v < u compatible with u
vector<vector<int>> smaller(LIMIT + 1);
auto addPrimitive = [&](int a, int b) {
if (a <= 0 || b <= 0) return;
int u = max(a, b), v = min(a, b);
for (int k = 1; (long long)k * (u + v) <= LIMIT; k++) {
smaller[k * u].push_back(k * v);
}
};
for (int m = 2; (long long)m * m < LIMIT; m++) {
for (int n = 1; n < m; n++) {
if (__gcd(m, n) != 1) continue;
if ((m - n) % 3 == 0) continue;
int a = m * m - n * n;
int b = 2 * m * n + n * n;
addPrimitive(a, b);
}
}
// Sort and deduplicate each adjacency list
for (int i = 0; i <= LIMIT; i++) {
sort(smaller[i].begin(), smaller[i].end());
smaller[i].erase(unique(smaller[i].begin(), smaller[i].end()), smaller[i].end());
}
// Find triangles
set<int> valid_sums;
for (int p = 1; p <= LIMIT; p++) {
if (smaller[p].empty()) continue;
// For quick lookup, use a set for smaller[p]
unordered_set<int> sp_set(smaller[p].begin(), smaller[p].end());
for (int q : smaller[p]) {
int max_r = LIMIT - p - q;
if (max_r <= 0) continue;
for (int r : smaller[q]) {
if (r >= q) continue;
if (r > max_r) continue;
if (sp_set.count(r)) {
valid_sums.insert(p + q + r);
}
}
}
}
long long ans = 0;
for (int s : valid_sums) ans += s;
cout << ans << endl;
return 0;
}
"""
Problem 143: Investigating the Torricelli Point of a Triangle
At the Torricelli point T, angles ATB = BTC = CTA = 120 degrees.
By cosine rule: a^2 = q^2 + qr + r^2, etc.
We need p, q, r positive integers with p^2+pq+q^2, p^2+pr+r^2, q^2+qr+r^2
all perfect squares, and p+q+r <= 120000.
120-compatible pairs: (u,v) with u^2+uv+v^2 a perfect square.
Primitive parametrization: u = m^2-n^2, v = 2mn+n^2
with m > n >= 1, gcd(m,n) = 1, (m-n) not divisible by 3.
Then u^2+uv+v^2 = (m^2+mn+n^2)^2.
All solutions are k*(u,v) for positive integer k.
"""
from math import gcd, isqrt
from collections import defaultdict
def solve():
LIMIT = 120000
# Build adjacency: smaller[u] = set of v < u compatible with u
smaller = defaultdict(set)
def add_primitive(a, b):
"""Add primitive pair (a,b) and all its multiples."""
if a <= 0 or b <= 0:
return
u, v = max(a, b), min(a, b)
k = 1
while k * (u + v) <= LIMIT:
smaller[k * u].add(k * v)
k += 1
# Generate all primitive pairs: (m^2-n^2, 2mn+n^2)
# with m > n >= 1, gcd(m,n) = 1, (m-n) % 3 != 0
m = 2
while m * m < LIMIT: # rough bound
for n in range(1, m):
if gcd(m, n) != 1:
continue
if (m - n) % 3 == 0:
continue
a = m * m - n * n
b = 2 * m * n + n * n
add_primitive(a, b)
m += 1
# Find triangles: p > q > r >= 1, all three pairs compatible, p+q+r <= LIMIT
valid_sums = set()
for p in sorted(smaller.keys()):
sp_set = smaller[p]
for q in sorted(sp_set, reverse=True):
max_r = LIMIT - p - q
if max_r <= 0:
continue
if q not in smaller:
continue
for r in smaller[q]:
if r >= q:
continue
if r > max_r:
continue
if r in sp_set:
valid_sums.add(p + q + r)
answer = sum(valid_sums)
print(answer)
if __name__ == '__main__':
solve()