Problem
The sequence of triangle numbers is generated by adding the natural numbers. So the th triangle number would be . The first ten terms would be:
Let us list the factors of the first seven triangle numbers:
We can see that 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?
Solution
#include <iostream>
int divisor_count(int n) {
int ret = 1;
for (int i = 2; i * i <= n; ++i) {
int count = 0;
while (n % i == 0) {
n /= i;
count++;
}
ret *= (count + 1);
}
return n > 1 ? ret * 2 : ret;
}
int main(void) {
int n = 0;
for (int i = 1;; ++i) {
n += i;
if (divisor_count(n) > 500) {
std::cout << n << std::endl;
break;
}
}
return 0;
}