Commit b29eb326 by Andrew Dahl

Solved Problem 12

parent aba01aae
Showing with 58 additions and 0 deletions
Question:
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 <vector>
using namespace std;
int main() {
long int triangle_number = 0;
int goal = 500;
vector<int> numbers;
for(int i = 1; true; i++)
{
triangle_number += i;
for(int j = 1; j < triangle_number/2; j++)
{
if(triangle_number % j == 0)
{
if(j == (triangle_number/j))
{
numbers.push_back(j);
break;
}
if(j > triangle_number/j)
break;
numbers.push_back(j);
numbers.push_back(triangle_number/j);
}
}
if(numbers.size() >= goal)
break;
else
numbers.clear();
}
cout << triangle_number << endl;
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment