# 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 = 12Output:2

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

Input:N = 20Output: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;` `}` |

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

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

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

## 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.` `?>` |

## Javascript

`<script>` `// Javascript 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` `n - parseInt(n / 2, 10) - parseInt(n / 3, 10) -` ` ` `parseInt(n / 5, 10) - parseInt(n / 7, 10) +` ` ` `parseInt(n / 6, 10) + parseInt(n / 10, 10) +` ` ` `parseInt(n / 14, 10) + parseInt(n / 15, 10) +` ` ` `parseInt(n / 21, 10) + parseInt(n / 35, 10) -` ` ` `parseInt(n / 30, 10) - parseInt(n / 42, 10) -` ` ` `parseInt(n / 70, 10) - parseInt(n / 105, 10) +` ` ` `parseInt(n / 210, 10);` `}` `// Driver code` `let n = 20;` `document.write(countNumbers(n));` `// This code is contributed by mukesh07` `</script>` |

**Output:**

5