All Euler problems
Project Euler

Highly Divisible Triangular Number

The n -th triangular number is T_n = (n(n+1))/(2). Find the smallest T_n with tau(T_n) > 500, where tau(m) denotes the number of positive divisors of m.

Source sync Apr 19, 2026
Problem #0012
Level Level 00
Solved By 240,261
Languages C++, Python
Answer 76576500
Length 319 words
number_theorymodular_arithmeticbrute_force

Problem Statement

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

The sequence of triangle numbers is generated by adding the natural numbers. So the $7^{th}$ triangle number would be $1 + 2 + 3 + 4 + 5 + 6 + 7 = 28$. The first ten terms would be: $$1, 3, 6, 10, 15, 21, 28, 36, 45, 55, \dots$$ Let us list the factors of the first seven triangle numbers: \begin{align*} \mathbf 1 &\colon 1\\ \mathbf 3 &\colon 1,3\\ \mathbf 6 &\colon 1,2,3,6\\ \mathbf{10} &\colon 1,2,5,10\\ \mathbf{15} &\colon 1,3,5,15\\ \mathbf{21} &\colon 1,3,7,21\\ \mathbf{28} &\colon 1,2,4,7,14,28 \end{align*} We can see that $28$ is the first triangle number to have over five divisors.

What is the value of the first triangle number to have over five hundred divisors?

Problem 12: Highly Divisible Triangular Number

Mathematical Development

Definitions and Notation

Definition 1. For mZ+m \in \mathbb{Z}^+ with prime factorization m=p1a1p2a2pkakm = p_1^{a_1} p_2^{a_2} \cdots p_k^{a_k}, the divisor function is τ(m)=#{dZ+:dm}\tau(m) = \#\{d \in \mathbb{Z}^+ : d \mid m\}.

Definition 2. The nn-th triangular number is Tn=i=1ni=n(n+1)2T_n = \sum_{i=1}^{n} i = \frac{n(n+1)}{2}.

Theorems

Theorem 1 (Divisor count formula). If m=p1a1pkakm = p_1^{a_1} \cdots p_k^{a_k}, then τ(m)=i=1k(ai+1)\tau(m) = \prod_{i=1}^{k}(a_i + 1).

Proof. A positive divisor of mm has the form i=1kpibi\prod_{i=1}^{k} p_i^{b_i} where 0biai0 \le b_i \le a_i. Each exponent bib_i admits ai+1a_i + 1 independent choices, yielding i=1k(ai+1)\prod_{i=1}^{k}(a_i + 1) divisors in total. \square

Theorem 2 (Multiplicativity of τ\tau). The function τ\tau is multiplicative: if gcd(a,b)=1\gcd(a,b) = 1, then τ(ab)=τ(a)τ(b)\tau(ab) = \tau(a)\tau(b).

Proof. Write a=ipiαia = \prod_i p_i^{\alpha_i} and b=jqjβjb = \prod_j q_j^{\beta_j}. Since gcd(a,b)=1\gcd(a,b) = 1, the sets {pi}\{p_i\} and {qj}\{q_j\} are disjoint. Hence ab=ipiαijqjβjab = \prod_i p_i^{\alpha_i} \cdot \prod_j q_j^{\beta_j} is in canonical form, and

τ(ab)=i(αi+1)j(βj+1)=τ(a)τ(b).\tau(ab) = \prod_i (\alpha_i + 1) \cdot \prod_j (\beta_j + 1) = \tau(a)\,\tau(b). \quad \square

Lemma 1 (Coprimality of consecutive integers). For all n1n \ge 1, gcd(n,n+1)=1\gcd(n, n+1) = 1.

Proof. If dnd \mid n and d(n+1)d \mid (n+1), then d(n+1)n=1d \mid (n+1) - n = 1, so d=1d = 1. \square

Theorem 3 (Coprime factorization of TnT_n). For all n1n \ge 1:

τ(Tn)={τ ⁣(n2)τ(n+1)if 2n,τ(n)τ ⁣(n+12)if 2n.\tau(T_n) = \begin{cases} \tau\!\left(\dfrac{n}{2}\right) \cdot \tau(n+1) & \text{if } 2 \mid n, \\[8pt] \tau(n) \cdot \tau\!\left(\dfrac{n+1}{2}\right) & \text{if } 2 \nmid n. \end{cases}

Proof. We have Tn=n(n+1)2T_n = \frac{n(n+1)}{2}. Exactly one of n,n+1n, n+1 is even. Write the factorization as Tn=ABT_n = A \cdot B where the factor of 22 is absorbed into the even term:

  • If 2n2 \mid n: set A=n/2A = n/2, B=n+1B = n+1.
  • If 2n2 \nmid n: set A=nA = n, B=(n+1)/2B = (n+1)/2.

We must verify gcd(A,B)=1\gcd(A,B) = 1 in each case. Consider the case 2n2 \mid n. Every prime pp dividing A=n/2A = n/2 also divides nn. By Lemma 1, gcd(n,n+1)=1\gcd(n, n+1) = 1, so pn+1=Bp \nmid n+1 = B. Hence gcd(A,B)=1\gcd(A,B) = 1. The odd case is symmetric. Applying Theorem 2 yields the result. \square

Remark. This decomposition is the key algorithmic insight: instead of factoring TnT_n (which can be as large as n2/2\sim n^2/2), we factor two numbers of size n/2\sim n/2 and n\sim n separately, reducing the trial division cost.

Editorial

We test triangle numbers in increasing order and compute their divisor counts from prime factorizations. For each index nn, we use Tn=n(n+1)/2T_n = n(n+1)/2 together with the coprimality of nn and n+1n+1 to evaluate τ(Tn)\tau(T_n) as τ(n/2)τ(n+1)\tau(n/2)\tau(n+1) or τ(n)τ((n+1)/2)\tau(n)\tau((n+1)/2), where the helper τ\tau factors its input by trial division. The first triangle number whose divisor count exceeds the target is therefore the desired answer.

Pseudocode

Algorithm: First Triangle Number with More Than M Divisors
Require: A divisor threshold M.
Ensure: The least triangle number T_n with tau(T_n) > M.
1: Initialize n ← 1.
2: Repeat:
3:     Factor the coprime pair n and n + 1, divide one factor by 2, and combine the resulting exponents to obtain tau(T_n).
4:     If tau(T_n) > M, return T_n ← n(n + 1) / 2; otherwise set n ← n + 1.

Complexity Analysis

Proposition. The algorithm terminates in O(NN)O(N\sqrt{N}) time and O(1)O(1) auxiliary space, where NN is the index satisfying τ(TN)>K\tau(T_N) > K.

Proof. For each nn from 11 to NN, we compute τ\tau of two numbers, each at most n+1n + 1. Trial division of an integer mm requires O(m)O(\sqrt{m}) divisions. Hence the cost per iteration is O(n)O(\sqrt{n}), and the total cost is

n=1NO(n)=O ⁣(1Nxdx)=O(N3/2)=O(NN).\sum_{n=1}^{N} O(\sqrt{n}) = O\!\left(\int_1^N \sqrt{x}\, dx\right) = O(N^{3/2}) = O(N\sqrt{N}).

No auxiliary data structure is needed beyond O(1)O(1) variables. \square

Verification. At n=12375n = 12375: T12375=76576500=2232537111317T_{12375} = 76\,576\,500 = 2^2 \cdot 3^2 \cdot 5^3 \cdot 7 \cdot 11 \cdot 13 \cdot 17, giving τ=3342222=576>500\tau = 3 \cdot 3 \cdot 4 \cdot 2 \cdot 2 \cdot 2 \cdot 2 = 576 > 500.

Answer

76576500\boxed{76576500}

Code

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

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

int countDivisors(long long n) {
    int count = 1;
    for (long long d = 2; d * d <= n; d++) {
        int e = 0;
        while (n % d == 0) { n /= d; e++; }
        count *= (e + 1);
    }
    if (n > 1) count *= 2;
    return count;
}

int main() {
    for (long long n = 1; ; n++) {
        long long a = (n % 2 == 0) ? n / 2 : n;
        long long b = (n % 2 == 0) ? n + 1 : (n + 1) / 2;
        if (countDivisors(a) * countDivisors(b) > 500) {
            cout << a * b << endl;
            return 0;
        }
    }
}