Tim Varley Logo
Tim Varley Principal AI Engineer and Tech Leader
Problem #12 medium

Highly Divisible Triangular Number

The sequence of triangle numbers is generated by adding the natural numbers. So the 7th 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,1, 3, 6, 10, 15, 21, 28, 36, 45, 55, \dots

Let us list the factors of the first seven triangle numbers:

\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?
View on Project Euler

Implementations

cpp
// https://projecteuler.net/problem=12
// The sequence of triangle numbers is generated by adding the natural numbers.
// So the 7th 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, ...
//
// Let us list the factors of the first seven triangle numbers:
//
// 1: 1
// 3: 1,3
// 6: 1,2,3,6
// 10: 1,2,5,10
// 15: 1,3,5,15
// 21: 1,3,7,21
// 28: 1,2,4,7,14,28
//
// 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?
// Answer: 76576500
#include <iostream>
#include <algorithm>
// @see http://en.wikipedia.org/wiki/Triangular_number
int triangle(int n)
{
if( 1 == n ){
return 3;
}
return ((n+1)*n)/2;
}
// Math fact: a prime decomposition n = Prod_i p_i^e_i yields
// Prod_i (e_i + 1) different divisors
int num_divisors(int n)
{
int divisors = 1;
// Evens
{
int count = 0;
while ( 0 == (n % 2)) {
++count;
n /= 2;
}
divisors *= (count + 1);
}
// Odds
for (size_t i = 3; i <= n; i += 2){
int count = 0;
while ( 0 == (n % i) ) {
++count;
n /= i;
}
divisors *= (count + 1);
}
return divisors;
}
int find_highest_divisible_triangular_number( int hurdle )
{
int divisor_count = 0;
int i = 1;
while( divisor_count <= hurdle ){
divisor_count = num_divisors(triangle(i));
i++;
}
return triangle(i-1);
}
#if ! defined UNITTEST_MODE
int main(int argc, char const *argv[])
{
std::cout << "Answer: " << find_highest_divisible_triangular_number(500) << std::endl;
}
#endif // #if ! defined UNITTEST_MODE
View on GitHub
O(n) time complexity

Solution Notes

Mathematical Background

Triangular numbers are generated by the formula Tn=n(n+1)2T_n = \frac{n(n+1)}{2}. They represent the sum of the first n natural numbers and form a quadratic sequence.

The number of divisor function d(n)d(n) counts how many positive divisors n has. For a number with prime factorization n=p1a1×p2a2××pkakn = p_1^{a_1} \times p_2^{a_2} \times \dots \times p_k^{a_k}, the number of divisors is d(n)=(a1+1)(a2+1)(ak+1)d(n) = (a_1 + 1)(a_2 + 1) \dots (a_k + 1).

Highly composite numbers (numbers with many divisors) often result from having many small prime factors. Triangle numbers can be highly composite because they may share factors with many other numbers.

Algorithm Analysis

The implementations combine two key components:

Triangle number generation: Tn=n(n+1)2T_n = \frac{n(n+1)}{2} computed incrementally or via formula.

Divisor counting: Factorize each triangle number and compute d(n)=(ei+1)d(n) = \prod (e_i + 1) where eie_i are exponents in prime factorization.

Optimization: Check divisors of TnT_n by factoring TnT_n directly, often more efficient than checking all numbers up to TnT_n.

Time complexity depends on factorization efficiency. For large triangle numbers, fast factorization methods become crucial.

Key Insights

  • Triangle numbers can be written as Tn=n(n+1)2T_n = \frac{n(n+1)}{2}, where n and n+1 are coprime
  • The number with the most divisors below a certain size is often a triangle number
  • 76576500 = T_{12375} has exactly 576 divisors (> 500)
  • The search requires checking increasingly large triangle numbers until one has >500 divisors
  • Most triangle numbers have relatively few divisors, but highly composite ones are well-distributed
  • This problem demonstrates the connection between additive sequences and multiplicative properties

Educational Value

This problem teaches fundamental concepts in:

  • Number theory and divisor functions
  • Prime factorization and its applications
  • The relationship between different number sequences
  • Algorithm efficiency for computational number theory
  • The mathematical properties of highly composite numbers
  • How additive and multiplicative properties interact in number sequences