From a0873eb070a80a66d21197ef0e8d9aee821ba2d3 Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Thu, 27 Sep 2018 05:13:20 -0400 Subject: [PATCH] Added a cpp for problem 1 --- ProjectEuler/C++/Problem1.cpp | 45 +++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 ProjectEuler/C++/Problem1.cpp diff --git a/ProjectEuler/C++/Problem1.cpp b/ProjectEuler/C++/Problem1.cpp new file mode 100644 index 0000000..447ed80 --- /dev/null +++ b/ProjectEuler/C++/Problem1.cpp @@ -0,0 +1,45 @@ +//ProjectEuler/C++/Problem1.cpp +//Matthew Ellison +// Created: 9-28-18 +//Modified: 9-28-18 +//What is the sum of all the multiples of 3 or 5 that are less than 1000 + + +#include +#include +#include + + +int main(){ + unsigned long fullSum = 0; //For the sum of all the numbers + std::vector numbers; //Holds all the numbers + + std::chrono::high_resolution_clock::time_point startTime = std::chrono::high_resolution_clock::now(); + //Step through every number < 1000 and see if either 3 or 5 divide it evenly + for(int cnt = 0;cnt < 1000;++cnt){ + //If either divides it then add it to the vector + if((cnt % 3) == 0){ + numbers.push_back(cnt); + } + else if((cnt % 5) == 0){ + numbers.push_back(cnt); + } + } + + //Get the sum of all numbers + for(unsigned long num : numbers){ + fullSum += num; + } + + //Calculate the time needed to run the algorithm + std::chrono::high_resolution_clock::time_point endTime = std::chrono::high_resolution_clock::now(); + std::chrono::high_resolution_clock::duration dur = std::chrono::duration_cast(endTime - startTime); + + //Print the output + std::cout << "The sum of all the numbers < 1000 that are divisible by 3 or 5 is " << fullSum + << "\nIt took " << dur.count() << " nanoseconds to run this algorithm" << std::endl; + + std::cin.get(); + + return 0; +}