# 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++

`// 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*

*filter_none*

## Java

`// 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*

*filter_none*

## Python3

`# 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*

*filter_none*

## C#

`// 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*

*filter_none*

## PHP

**Output:**

5

## Recommended Posts:

- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Sum of all numbers divisible by 6 in a given range
- Count numbers in range 1 to N which are divisible by X but not by Y
- Count the numbers divisible by 'M' in a given range
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Count of Numbers in a Range divisible by m and having digit d in even positions
- Count numbers in a range that are divisible by all array elements
- Sum of largest divisible powers of p (a prime number) in a range
- Sum of n digit numbers divisible by a given number
- Smallest number divisible by first n numbers
- Smallest n digit number divisible by given three numbers
- Program to print all the numbers divisible by 3 and 5 for a given number
- Count n digit numbers divisible by given number
- Largest N digit number divisible by given three numbers

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.