mirror of
https://bitbucket.org/Mattrixwv/octavefunctions.git
synced 2025-12-07 03:03:57 -05:00
42 lines
1.2 KiB
C++
42 lines
1.2 KiB
C++
#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;
|
|
}
|