Count numbers which are divisible by all the numbers from 2 to 10 in C++


We are given a number let’s say, num and the task is to calculate the count of numbers in the range 1 to num that are divisible by 2, 3, 4, 5, 6, 7, 8, 9 and 10.

Input − int num = 10000

Output − Count numbers which are divisible by all the numbers from 2 to 10 are: 3

Explanation − There are 3 numbers from 1 to 10000 that are divisible by all the numbers starting from 2 till 10 and those are −

2520-: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 18, 20, 21, 24, 28, 30, 35, 36, 40, 42, 45, 56, 60, 63, 70, 72, 84, 90, 105, 120, 126, 140, 168, 180, 210, 252, 280, 315, 360, 420, 504, 630, 840, 1260, 2520.
5040-: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 28, 30, 35, 36, 40, 42, 45, 48, 56, 60, 63, 70, 72, 80, 84, 90, 105, 112, 120, 126, 140, 144, 168, 180, 210, 240, 252, 280, 315, 336, 360, 420, 504, 560, 630, 720, 840, 1008, 1260, 1680, 2520, 5040
7560-: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 18, 20, 21, 24, 27, 28, 30, 35, 36, 40, 42, 45, 54, 56, 60, 63, 70, 72, 84, 90, 105, 108, 120, 126, 135, 140, 168, 180, 189, 210, 216, 252, 270, 280, 315, 360, 378, 420, 504, 540, 630, 756, 840, 945, 1080, 1260, 1512, 1890, 2520, 3780.

Input − int num = 20000

Output − Count numbers which are divisible by all the numbers from 2 to 10 are − 3

Explanation − There are 7 numbers from 1 to 10000 that are divisible by all the numbers starting from 2 till 10 and those are − 2520, 5040, 7560, 10080, 12600, 15120 and 17640,

Approach used in the below program is as follows

There can be multiple approaches to solve the given problem i.e. naive approach and efficient approach. So let’s first look at the naive approach.

  • Input the number let’s say num

  • Take an array and store all the numbers from 2 to 10 inside an integer array of fixed length which is 9.

  • Take temporary variables first is count to store the total of numbers and another is flag to check whether the number is divisible or not.

  • Start loop For from i to 1 till the num

  • Inside the loop, set num to i and index to 0

  • Start while till index is less than 9 i.e. size of an array

  • Check IF num % arr[index++] == 0 then set flag as 1 Else set flag as 0

  • Check IF flag is 1 then increment the count by 1

  • Return count

  • Print the result.

Efficient approach

As we can see there is a pattern in the numbers that is divisible by all the numbers from 2 to 10.

The smallest number which is divisible by all the numbers from 2 to 10 is 2520

5 * 7 * 8 * 9 = 2520(n = 1)
5 * 7 * 8 * 9 * 2 = 5040(n = 2)
5 * 7 * 8 * 9 * 3 = 7560(n = 3)
.
.

As we can see 2520 is the common factor of all the numbers divisible by 2, 3, 4, 5, 6, 7, 8, 9, 10. so , if we divide the given number by 2520 we will get our result.

Code-1(naive approach)

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int count(int num){
   int count = 0;
   int flag=0;
   int index=0;
   int arr[9] = {2, 3, 4, 5, 6, 7, 8, 9, 10 };
   for (int i = 1; i <= num; i++){
      int num = i;
      index=0;
      while(index<9){
         if(num % arr[index++] == 0){
            flag=1;
         }
         else{
            flag=0;
            break;
         }
      }
      if (flag == 1){
         count++;
      }
   }
   return count;
}
int main(){
   int num = 10000;
   cout<<"Count numbers which are divisible by all the numbers from 2 to 10 are: "<<count(num);
return 0;
}

Output

If we run the above code it will generate the following output −

Count numbers which are divisible by all the numbers from 2 to 10 are: 3

Code-2(Efficient Approach)

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   int num = 10000;
   int count = num / 2520;
   cout<<"Count numbers which are divisible by all the numbers from 2 to 10 are: "<<count;
   return 0;
}

Output

If we run the above code it will generate the following output −

Count numbers which are divisible by all the numbers from 2 to 10 are: 3

Updated on: 31-Oct-2020

293 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements