Expressing an Integer as the Sum of Triangular Numbers
Let T(n) denote the number of ordered triples (a, b, c) of non-negative integers such that (a(a+1))/(2) + (b(b+1))/(2) + (c(c+1))/(2) = n. Find sum_i T(n_i) for specified values n_i (Project Euler...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Gauss famously proved that every positive integer can be expressed as the sum of three
Let \(G(n)\) be the number of ways of expressing \(n\) as the sum of three triangular numbers, regarding different arrangements of the terms of the sum as distinct.
For example, \(G(9) = 7\), as \(9\) can be expressed as: \(3+3+3\), \(0+3+6\), \(0+6+3\), \(3+0+6\), \(3+6+0\), \(6+0+3\), \(6+3+0\).
You are given \(G(1000) = 78\) and \(G(10^6) = 2106\).
Find \(G(17526 \times 10^9)\).
Problem 621: Expressing an Integer as the Sum of Triangular Numbers
Mathematical Foundation
Theorem 1 (Reduction to Sums of Odd Squares). For every non-negative integer , equals the number of ordered triples of odd positive integers satisfying .
Proof. Denote the -th triangular number by . Observe that
The map is a bijection from to the set of odd positive integers. Given a triple with , summing the identity over yields
Conversely, if are odd positive integers with , then , , for unique non-negative integers , and reversing the computation gives . Since the correspondence preserves the ordering of the triple, it is a bijection between the sets counted by and by the odd-square representation count.
Theorem 2 (Gauss’s Eureka Theorem). Every non-negative integer is the sum of three triangular numbers; equivalently, for all .
Proof. By Theorem 1, if and only if is representable as a sum of three squares of odd positive integers. We proceed in two steps.
Step 1: is a sum of three squares. By Legendre’s three-square theorem, a positive integer fails to be a sum of three squares if and only if for some non-negative integers . Since , we have , and is odd (hence not divisible by 4). Therefore is not of the excluded form, so it is a sum of three squares.
Step 2: All three squares must be odd. Let . Modulo 4, every square is congruent to 0 or 1. Since , all three of must be , which forces to be odd. In particular, , so they are odd positive integers (after choosing appropriate signs).
Lemma 1 (Hurwitz Class Number Formula). For a positive integer , let . Then
where is the Hurwitz class number of .
Proof. This is a classical result of Gauss and Dirichlet. See Grosswald, Representations of Integers as Sums of Squares, Chapter 8. The connection arises because counts lattice points on a sphere of radius , and this count is expressible in terms of the class number of the imaginary quadratic order of discriminant .
Theorem 3 (Extraction of ). For every non-negative integer ,
Proof. By Theorem 1, counts ordered triples of odd positive integers with . By Step 2 of Theorem 2, every integer representation of as a sum of three squares has all three entries odd. Each such triple with all entries nonzero and distinct in absolute value gives rise to exactly signed triples , of which exactly one lies in the positive octant. Since , we claim no two of can be equal: if , then gives , a contradiction since no square is (as ). Likewise and are excluded. Therefore the -to- correspondence is exact, and .
Editorial
Count ordered triples (a,b,c) with t_a + t_b + t_c = n. We enumerate the admissible parameter range, discard candidates that violate the derived bounds or arithmetic constraints, and update the final set or total whenever a candidate passes the acceptance test.
Pseudocode
m = 8*n + 3
count = 0
For a from 0 to floor((sqrt(8*n+1) - 1) / 2):
remainder = n - a*(a+1)/2
For b from 0 to floor((sqrt(8*remainder+1) - 1) / 2):
remainder2 = remainder - b*(b+1)/2
disc = 8*remainder2 + 1
s = isqrt(disc)
If s*s == disc and s % 2 == 1 then
count += 1
Return count
Complexity Analysis
- Time: for the brute-force double loop (each loop runs up to ). The analytic method via with the Hurwitz class number runs in , dominated by factoring .
- Space: auxiliary for brute force; for sieve-based factorization in the analytic method.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
bool is_triangular(long long m) {
if (m < 0) return false;
long long d = 8LL * m + 1;
long long s = (long long)sqrt((double)d);
for (long long c = s - 1; c <= s + 1; c++)
if (c >= 0 && c * c == d) return true;
return false;
}
int main() {
int N = 80;
long long total = 0;
for (int n = 0; n <= N; n++) {
int count = 0;
for (int a = 0; (long long)a * (a + 1) / 2 <= n; a++) {
long long ta = (long long)a * (a + 1) / 2;
for (int b = 0; (long long)b * (b + 1) / 2 <= n - ta; b++) {
long long tb = (long long)b * (b + 1) / 2;
if (is_triangular(n - ta - tb)) count++;
}
}
total += count;
}
cout << total << endl;
return 0;
}
"""
Problem 621: Expressing n as Sum of Triangular Numbers
Count ordered triples (a,b,c) with t_a + t_b + t_c = n.
"""
def is_triangular(m):
"""Check if m is a triangular number."""
if m < 0:
return False
d = 8 * m + 1
s = int(d**0.5)
for c in [s - 1, s, s + 1]:
if c >= 0 and c * c == d:
return True
return False
def triangular(k):
return k * (k + 1) // 2
def count_representations(n):
"""Count ordered triples (a,b,c) with t_a + t_b + t_c = n."""
count = 0
a = 0
while triangular(a) <= n:
ta = triangular(a)
b = 0
while triangular(b) <= n - ta:
tb = triangular(b)
if is_triangular(n - ta - tb):
count += 1
b += 1
a += 1
return count
# Compute for small range
N = 80
counts = [count_representations(k) for k in range(N + 1)]
total = sum(counts)
print(f"Sum of T(k) for k=0..{N}: {total}")
for k in range(min(20, N + 1)):
print(f" T({k}) = {counts[k]}")