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:
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?
Note from the author: The code works, although it needs really heavy optimization.
#include <iostream>
#include <limits>
using namespace std;
void pause() { cin.ignore(numeric_limits<streamsize>::max(), '\n'); }
long long int generateTriangleNumber(long long int generation)
{
long long int sum = 0;
for (long long int i = 1; i <= generation; i++)
{
sum += i;
}
return sum;
}
int checkNumberofDivisors(long long int number)
{
int divisorCount = 0;
for (long long int i = 1; i <= number; i++)
{
if (number % i == 0)
{
divisorCount++;
}
}
return divisorCount;
}
int main()
{
long int iterator = 1;
long long int triangleNumber;
int numberofDivisors = 500;
int numDivis;
bool notOver500 = true;
while (notOver500)
{
triangleNumber = generateTriangleNumber(iterator);
numDivis = checkNumberofDivisors(triangleNumber);
//cout << triangleNumber << " " << numDivis << endl;
if (numDivis >= numberofDivisors)
notOver500 = false;
else
iterator += 1;
}
cout << triangleNumber;
pause();
}