diff --git a/ProjectEuler/C++/Problem12.cpp b/ProjectEuler/C++/Problem12.cpp new file mode 100644 index 0000000..3ee0185 --- /dev/null +++ b/ProjectEuler/C++/Problem12.cpp @@ -0,0 +1,41 @@ +#include +#include +#include + +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::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; +}