# 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

`<?php ` `// PHP 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] ` `function` `countNumbers(` `$n` `) ` `{ ` ` ` `return` `(int)(` `$n` `- ` `$n` `/ 2) - (int)(` `$n` `/ 3 ) - ` ` ` `(int)(` `$n` `/ 5 ) - (int)(` `$n` `/ 7) + ` ` ` `(int)(` `$n` `/ 6 ) + (int)(` `$n` `/ 10) + ` ` ` `(int)(` `$n` `/ 14) + (int)(` `$n` `/ 15) + ` ` ` `(int)(` `$n` `/ 21) + (int)(` `$n` `/ 35) - ` ` ` `(int)(` `$n` `/ 30) - (int)(` `$n` `/ 42) - ` ` ` `(int)(` `$n` `/ 70) - (int)(` `$n` `/ 105) + ` ` ` `(int)(` `$n` `/ 210); ` `} ` ` ` `// Driver code ` `$n` `= 20; ` `echo` `(countNumbers(` `$n` `)); ` ` ` `// This code is contributed by Code_Mech. ` `?> ` |

*chevron_right*

*filter_none*

**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 in the given range which are 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
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- 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 that does not contain the digit M and which is divisible by M.
- Count numbers in a range that are divisible by all array elements
- Count of numbers in range which are divisible by M and have digit D at odd places
- Count of Numbers in a Range divisible by m and having digit d in even positions
- Count numbers divisible by K in a range with Fibonacci digit sum for Q queries
- Number of integers in a range [L, R] which are divisible by exactly K of it's digits
- Sum of largest divisible powers of p (a prime number) in a range

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.