This commit is contained in:
2018-09-26 23:33:52 -04:00

View File

@@ -0,0 +1,41 @@
#include <iostream>
#include <cmath>
#include <chrono>
unsigned long countDivisors(unsigned long number);
int main(){
bool found = false;
unsigned long sum = 1;
unsigned long counter = 2;
const unsigned long goalDivisors = 500;
std::chrono::high_resolution_clock::time_point startTime = std::chrono::high_resolution_clock::now();
while(!found){
if(countDivisors(sum) > goalDivisors){
found = true;
}
else{
sum += counter;
++counter;
}
}
std::chrono::high_resolution_clock::time_point endTime = std::chrono::high_resolution_clock::now();
--counter;
std::cout << "The triangular number " << sum << " is made with all number >= " << counter << " and has " << countDivisors << " divisors" << std::endl;
std::cout << "The problem took " << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::duration(endTime - startTime)).count() << " milliseconds" << std::endl;
return 0;
}
unsigned long countDivisors(unsigned long number){
unsigned long numDivisors = 0;
for(int cnt = 0;cnt * cnt < number;++cnt){
//if((number % cnt) == 0){
if(fmod((double)number, (double)cnt) == 0){
numDivisors += 2;
}
}
return numDivisors;
}