All Euler problems
Project Euler

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...

Source sync Apr 19, 2026
Problem #0621
Level Level 20
Solved By 650
Languages C++, Python
Answer 11429712
Length 525 words
analytic_mathmodular_arithmeticalgebra

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 triangular numbers (including \(0\) as the lowest triangular number). In fact most numbers can be expressed as a sum of three triangular numbers in several ways.

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 nn, T(n)T(n) equals the number of ordered triples (x,y,z)(x, y, z) of odd positive integers satisfying x2+y2+z2=8n+3x^2 + y^2 + z^2 = 8n + 3.

Proof. Denote the kk-th triangular number by tk=k(k+1)/2t_k = k(k+1)/2. Observe that

8tk+1=4k2+4k+1=(2k+1)2.8t_k + 1 = 4k^2 + 4k + 1 = (2k+1)^2.

The map φ:k2k+1\varphi : k \mapsto 2k + 1 is a bijection from Z0\mathbb{Z}_{\ge 0} to the set of odd positive integers. Given a triple (a,b,c)(a, b, c) with ta+tb+tc=nt_a + t_b + t_c = n, summing the identity 8tk+1=(2k+1)28t_k + 1 = (2k+1)^2 over k{a,b,c}k \in \{a, b, c\} yields

8n+3=(2a+1)2+(2b+1)2+(2c+1)2.8n + 3 = (2a+1)^2 + (2b+1)^2 + (2c+1)^2.

Conversely, if (x,y,z)(x, y, z) are odd positive integers with x2+y2+z2=8n+3x^2 + y^2 + z^2 = 8n + 3, then x=2a+1x = 2a+1, y=2b+1y = 2b+1, z=2c+1z = 2c+1 for unique non-negative integers a,b,ca, b, c, and reversing the computation gives ta+tb+tc=nt_a + t_b + t_c = n. Since the correspondence (a,b,c)(x,y,z)(a,b,c) \leftrightarrow (x,y,z) preserves the ordering of the triple, it is a bijection between the sets counted by T(n)T(n) and by the odd-square representation count. \square

Theorem 2 (Gauss’s Eureka Theorem). Every non-negative integer is the sum of three triangular numbers; equivalently, T(n)1T(n) \ge 1 for all n0n \ge 0.

Proof. By Theorem 1, T(n)1T(n) \ge 1 if and only if 8n+38n + 3 is representable as a sum of three squares of odd positive integers. We proceed in two steps.

Step 1: 8n+38n+3 is a sum of three squares. By Legendre’s three-square theorem, a positive integer mm fails to be a sum of three squares if and only if m=4a(8b+7)m = 4^a(8b + 7) for some non-negative integers a,ba, b. Since 8n+33(mod8)8n + 3 \equiv 3 \pmod{8}, we have 8n+3≢7(mod8)8n + 3 \not\equiv 7 \pmod{8}, and 8n+38n + 3 is odd (hence not divisible by 4). Therefore 8n+38n + 3 is not of the excluded form, so it is a sum of three squares.

Step 2: All three squares must be odd. Let 8n+3=x2+y2+z28n + 3 = x^2 + y^2 + z^2. Modulo 4, every square is congruent to 0 or 1. Since 8n+33(mod4)8n + 3 \equiv 3 \pmod{4}, all three of x2,y2,z2x^2, y^2, z^2 must be 1(mod4)\equiv 1 \pmod{4}, which forces x,y,zx, y, z to be odd. In particular, x,y,z0x, y, z \ne 0, so they are odd positive integers (after choosing appropriate signs). \square

Lemma 1 (Hurwitz Class Number Formula). For a positive integer mm, let r3(m)={(x,y,z)Z3:x2+y2+z2=m}r_3(m) = |\{(x,y,z) \in \mathbb{Z}^3 : x^2 + y^2 + z^2 = m\}|. Then

r3(m)=12H(m),r_3(m) = 12\,H(m),

where H(m)H(m) is the Hurwitz class number of mm.

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 r3(m)r_3(m) counts lattice points on a sphere of radius m\sqrt{m}, and this count is expressible in terms of the class number of the imaginary quadratic order of discriminant 4m-4m. \square

Theorem 3 (Extraction of T(n)T(n)). For every non-negative integer nn,

T(n)=r3(8n+3)8.T(n) = \frac{r_3(8n+3)}{8}.

Proof. By Theorem 1, T(n)T(n) counts ordered triples of odd positive integers (x,y,z)(x,y,z) with x2+y2+z2=8n+3x^2 + y^2 + z^2 = 8n+3. By Step 2 of Theorem 2, every integer representation (x,y,z)Z3(x,y,z) \in \mathbb{Z}^3 of 8n+38n+3 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 23=82^3 = 8 signed triples (±x,±y,±z)(\pm x, \pm y, \pm z), of which exactly one lies in the positive octant. Since 8n+33(mod4)8n+3 \equiv 3 \pmod{4}, we claim no two of x,y,z|x|, |y|, |z| can be equal: if x2=y2x^2 = y^2, then 2x2+z2=8n+32x^2 + z^2 = 8n+3 gives z23(mod4)z^2 \equiv 3 \pmod{4}, a contradiction since no square is 3(mod4)\equiv 3 \pmod{4} (as 2x22(mod4)2x^2 \equiv 2 \pmod{4}). Likewise x=z|x| = |z| and y=z|y| = |z| are excluded. Therefore the 88-to-11 correspondence is exact, and T(n)=r3(8n+3)/8T(n) = r_3(8n+3)/8. \square

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: O(n)O(n) for the brute-force double loop (each loop runs up to O(n)O(\sqrt{n})). The analytic method via T(n)=r3(8n+3)/8T(n) = r_3(8n+3)/8 with the Hurwitz class number runs in O(n1/2+ε)O(n^{1/2+\varepsilon}), dominated by factoring 8n+38n+3.
  • Space: O(1)O(1) auxiliary for brute force; O(n1/2)O(n^{1/2}) for sieve-based factorization in the analytic method.

Answer

11429712\boxed{11429712}

Code

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

C++ project_euler/problem_621/solution.cpp
#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;
}