Advertisement
makispaiktis

Project Euler 1 - Multiples of 3 and 5

Apr 11th, 2020 (edited)
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 KB | None | 0 0
  1. /*
  2. If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
  3. Find the sum of all the multiples of 3 or 5 below 1000.
  4. */
  5.  
  6. #include <iostream>
  7. #include <stdlib.h>
  8. #include <vector>
  9. #include <algorithm>
  10.  
  11. using namespace std;
  12.  
  13. int main()
  14. {
  15.     vector <int> multiples = vector <int> ();
  16.     for(int i=3; i<1000; i++){
  17.         if(i % 3 ==0 || i % 5 == 0){
  18.             multiples.push_back(i);
  19.         }
  20.     }
  21.     int sum = 0;
  22.     for(unsigned int i =0; i<multiples.size(); i++){
  23.         sum += multiples[i];
  24.     }
  25.     cout << "Sum = " << sum << endl;
  26.     return 0;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement