mirror of
https://bitbucket.org/Mattrixwv/octavefunctions.git
synced 2025-12-07 03:03:57 -05:00
Added a cpp for problem 1
This commit is contained in:
45
ProjectEuler/C++/Problem1.cpp
Normal file
45
ProjectEuler/C++/Problem1.cpp
Normal file
@@ -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 <iostream>
|
||||
#include <vector>
|
||||
#include <chrono>
|
||||
|
||||
|
||||
int main(){
|
||||
unsigned long fullSum = 0; //For the sum of all the numbers
|
||||
std::vector<unsigned long> 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<std::chrono::nanoseconds>(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;
|
||||
}
|
||||
Reference in New Issue
Block a user