Advertisement
makispaiktis

Project Euler 5 - Smallest Multiple

Apr 25th, 2020 (edited)
651
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. /*
  2. 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
  3. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
  4. */
  5. #include <iostream>
  6. #include <stdlib.h>
  7. #include <math.h>
  8. #include <bits/stdc++.h>
  9. #define N 20
  10. #define LIMIT INT_MAX
  11.  
  12. using namespace std;
  13.  
  14. int main()
  15. {
  16.     // Create a vector that contains all the numbers from 1 to N
  17.     vector <int> numbers = vector <int> ();
  18.     for(int i=0; i<N; i++){
  19.         numbers.push_back(i+1);
  20.     }
  21.     // numbers = [1, 2, 3, .... , N-1, N]
  22.     int answer = 0;
  23.     for(int i=1; i<LIMIT; i++){
  24.         bool flag = false;
  25.         unsigned int counter = 0;
  26.         for(unsigned int j=0; j<numbers.size(); j++){
  27.             if(i % numbers[j] == 0){
  28.                 counter++;
  29.             }
  30.         }
  31.         if(counter == numbers.size()){
  32.             flag = true;
  33.         }
  34.         if(flag == true){
  35.             answer = i;
  36.             break;
  37.         }
  38.     }
  39.  
  40.     cout << "The smallest positive number that is evenly divisible by all of the numbers from 1 to " << N << " is: " << answer << endl;
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement