mirror of
https://bitbucket.org/Mattrixwv/octavefunctions.git
synced 2025-12-07 11:13:57 -05:00
Added a C++ for Problem 12
This commit is contained in:
41
ProjectEuler/C++/Problem12.cpp
Normal file
41
ProjectEuler/C++/Problem12.cpp
Normal 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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user