Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
- What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
- */
- #include <iostream>
- #include <stdlib.h>
- #include <math.h>
- #include <bits/stdc++.h>
- #define N 20
- #define LIMIT INT_MAX
- using namespace std;
- int main()
- {
- // Create a vector that contains all the numbers from 1 to N
- vector <int> numbers = vector <int> ();
- for(int i=0; i<N; i++){
- numbers.push_back(i+1);
- }
- // numbers = [1, 2, 3, .... , N-1, N]
- int answer = 0;
- for(int i=1; i<LIMIT; i++){
- bool flag = false;
- unsigned int counter = 0;
- for(unsigned int j=0; j<numbers.size(); j++){
- if(i % numbers[j] == 0){
- counter++;
- }
- }
- if(counter == numbers.size()){
- flag = true;
- }
- if(flag == true){
- answer = i;
- break;
- }
- }
- cout << "The smallest positive number that is evenly divisible by all of the numbers from 1 to " << N << " is: " << answer << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement