Numbers that are not divisible by any number in the range [2, 10]

Given an integer N. The task is to find the count of all the numbers from 1 to N which are not divisible by any number in the range [2, 10].

Examples:

Input: N = 12
Output: 2
1, 11 are the only numbers in range [1, 12] which are not divisible by any number from 2 to 10

Input: N = 20
Output: 5

Approach: Total numbers from 1 to n which are not divisible by any number from 2 to 10 is equal to n minus the numbers which are divisible by some numbers from 2 to 10.

The set of numbers which are divisible by some numbers from 2 to 10 can be found as union of the set of numbers from 1 to n divisible by 2, the set of numbers divisible by 3 and so on till 10.

Note that sets of numbers divisible by 4 or 6 or 8 are subsets of the set of numbers divisible by 2, and sets of numbers divisible by 6 or 9 are subsets of the set of numbers divisible by 3. So there is no need to unite 9 sets, it is enough to unite sets for 2, 3, 5 and 7 only.

The size of the set of numbers from 1 to n divisible by 2, 3, 5, 7 can be calculated using inclusion-exclusion principle that says that size of each single set should be added, size of pairwise intersections should be subtracted, size of all intersections of three sets should be added and so on.

The size of the set of numbers from 1 to n divisible by 2 is equal to ⌊n / 2⌋, the size of the set of numbers from 1 to n divisible by 2 and 3 is equal to ⌊n / (2 * 3)⌋ and so on.

So, the formula is n – ⌊n / 2⌋ – ⌊n / 3⌋ – ⌊n / 5⌋ – ⌊n / 7⌋ + ⌊n / (2 * 3)] + ⌊n / (2 * 5)] + ⌊n / (2 * 7)] + ⌊n / (3 * 5)] + ⌊n / (3 * 7)] + ⌊n / (5 * 7)] – ⌊n / (2 * 3 * 5)] – ⌊n / (2 * 3 * 7)] – ⌊n / (2 * 5 * 7)] – ⌊n / (3 * 5 * 7)]+ ⌊n / (2 * 3 * 5 * 7)]

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the count of numbers
// from 1 to N which are not divisible by
// any number in the range [2, 10]
int countNumbers(int n)
{
    return n - n / 2 - n / 3 - n / 5 - n / 7
           + n / 6 + n / 10 + n / 14 + n / 15 + n / 21 + n / 35
           - n / 30 - n / 42 - n / 70 - n / 105 + n / 210;
}
  
// Driver code
int main()
{
    int n = 20;
    cout << countNumbers(n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG
{
      
// Function to return the count of numbers
// from 1 to N which are not divisible by
// any number in the range [2, 10]
static int countNumbers(int n)
{
    return n - n / 2 - n / 3 - n / 5 - n / 7
        + n / 6 + n / 10 + n / 14 + n / 15 + n / 21 + n / 35
        - n / 30 - n / 42 - n / 70 - n / 105 + n / 210;
}
  
// Driver code
public static void main (String[] args) 
{
    int n = 20;
    System.out.println(countNumbers(n));
}
}
  
// This code is contributed by mits

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to return the count of numbers
# from 1 to N which are not divisible by
# any number in the range [2, 10]
def countNumbers(n):
    return (n - n // 2 - n // 3 - n // 5 - n // 7 + 
             n // 6 + n // 10 + n // 14 + n // 15 +
             n // 21 + n // 35 - n // 30 - n // 42 - 
             n // 70 - n // 105 + n // 210)
  
# Driver code
if __name__ == '__main__':
    n = 20
    print(countNumbers(n))
  
# This code contributed by Rajput-Ji

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
      
// Function to return the count of numbers
// from 1 to N which are not divisible by
// any number in the range [2, 10]
static int countNumbers(int n)
{
    return n - n / 2 - n / 3 - n / 5 - n / 7
        + n / 6 + n / 10 + n / 14 + n / 15 + n / 21 + n / 35
        - n / 30 - n / 42 - n / 70 - n / 105 + n / 210;
}
  
// Driver code
static void Main()
{
    int n = 20;
    Console.WriteLine(countNumbers(n));
}
}
  
// This code is contributed by mits

chevron_right


PHP

Output:

5


My Personal Notes arrow_drop_up

pawanasipugmailcom

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.