-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheuler012.cpp
More file actions
executable file
·32 lines (28 loc) · 935 Bytes
/
euler012.cpp
File metadata and controls
executable file
·32 lines (28 loc) · 935 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*******************************************************************************
// Euler 12
// Highly divisible triangular number
// Author: Eugene Kolo - 2014
// Contact: www.eugenekolo.com
// http://projecteuler.net/problem=12
*******************************************************************************/
#include <iostream>
#include <cmath>
int main () {
int nth = 1;
int triNum = 0;
int numOfDivisors = 0;
while (numOfDivisors <= 500) {
numOfDivisors = 0;
// nth triNum = (n-1)th triNum + n, meaning: 7th triNum = 6th triNum + 7
triNum = triNum + nth;
for (int i = 2; i <= floor(sqrt(triNum)); i++) {
if (triNum % i == 0) {
numOfDivisors = numOfDivisors + 2;
}
}
numOfDivisors = numOfDivisors + 2; // Account for (1,triNum) divisor pair
nth++;
}
std::cout << triNum << std::endl;
return 0;
}