- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find the Numbers that are not divisible by any number in the range [2, 10] using C++

In this article, we will discuss the problem to find the numbers from 1 to n(given) which are can not be divided by any number from 2 to 10. Let's understand this with some examples −

Input : num = 14 Output : 3 Explanation: There are three numbers, 1, 11, and 13, which are not divisible. Input : num = 21 Output : 5 Explanation: There are five numbers 1, 11, 13, 17, and 19, which are not divisible.

## Approach to find The Solution

### Simple Approach

If we check every number from 1 to num, whether it can be divided by any number from 2 to 10. If no, then increment the count. But this approach will take too much time hence increasing the time complexity.

### Efficient Approach

The best approach we can think of is first to find the numbers from 1 to num, divisible by any number in the range [ 2, 10 ], then subtract this count with num.

So first, we need to find all the numbers divisible by 2, 3, 4, 5,10. But numbers divisible by 4, 6, 8, and 10 are divisible by 2, and numbers divisible by three are divisible by 6 and 9.

We need to find all the numbers divisible by 2, 3, 5, and 7. And this we can calculate from the inclusion-exclusion principle.

### Inclusion-exclusion Principle

It states that we should include every single set's size, you should remove pairwise intersection's size, all intersection's size of three sets should be added, and so on.

Formula to find all numbers is,

= NUM – X + Y – Z + A.

Where,

X = num divisible by 2, 3, 5, 7 ( [num / 2] + [num / 3] + [num / 5] + [num / 7] ) Y = num divisible by (2,3), (2, 5), (2, 7), (3, 5), (3, 5), (3, 7) and (5, 7) = ( [num / (2 * 3)] + [num / (2 * 5)] + [num / (2 * 7)] + [num / (3 * 5)] + num / (3 * 7)] + [num / (5 * 7)] ). Z = num divisible by (2, 3, 5), (2, 3, 7), (2, 5, 7) and (3, 5, 7) = ( [num / (2 * 3 * 5)] + [num / (2 * 3 * 7)] + [num / (2 * 5 * 7)] + [num / (3 * 5 * 7)] ) A = num divisible by (2, 3, 5, 7) = ( [num / (2 * 3 * 5 * 7)] )

## Example

#include <bits/stdc++.h> using namespace std; int main() { int n = 21, result; // applying formula from inclusion - exclusion principle // to find the count of numbers not divisible by any number from 2 to 10. result = 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; cout << "The count of numbers, not div by [2, 10] is: " << result; return 0; }

## Output

The count of numbers, not div by [2, 10] is: 5

## Conclusion

In this article, we discussed the way to find non-divisible numbers from 2 to n. To solve this problem, we discussed the inclusion-exclusion principle. We also discussed the C++ program to apply the approach to get the result with O(1) complexity. You can write this program in any other language like Java, C, Python, etc. We hope you find this article helpful.

- Related Questions & Answers
- How to find numbers that are divisible by a certain number for a range of values in R?
- Finding the count of numbers divisible by a number within a range using JavaScript
- Count numbers in a range that are divisible by all array elements in C++
- Count numbers in range 1 to N which are divisible by X but not by Y in C++
- Elements of an array that are not divisible by any element of another array in C++
- Count numbers in range that are divisible by all of its non-zero digits in C++
- Smallest number that is divisible by first n numbers in JavaScript
- Check if any large number is divisible by 17 or not in Python
- Check if any large number is divisible by 19 or not in Python
- Golang Program to Find the Numbers which are Divisible by 7 and Multiple of 5 in a Given Range
- Find nth number that contains the digit k or divisible by k in C++
- How to find whether the Number is Divisible by 2 using C#?
- Python Program to Print all Numbers in a Range Divisible by a Given Number
- Golang Program to Print all Numbers in a Range Divisible by a Given Number
- Count the numbers divisible by ‘M’ in a given range in C++