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:
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?Implementations
// 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_numberint 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 divisorsint 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_MODEint main(int argc, char const *argv[]){ std::cout << "Answer: " << find_highest_divisible_triangular_number(500) << std::endl;}#endif // #if ! defined UNITTEST_MODESolution Notes
Mathematical Background
Triangular numbers are generated by the formula . They represent the sum of the first n natural numbers and form a quadratic sequence.
The number of divisor function counts how many positive divisors n has. For a number with prime factorization , the number of divisors is .
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: computed incrementally or via formula.
Divisor counting: Factorize each triangle number and compute where are exponents in prime factorization.
Optimization: Check divisors of by factoring directly, often more efficient than checking all numbers up to .
Time complexity depends on factorization efficiency. For large triangle numbers, fast factorization methods become crucial.
Key Insights
- Triangle numbers can be written as , 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