- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# 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 Articles
- 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++
- Count numbers in range that are divisible by all of its non-zero digits in C++
- A number is divisible by $12$. By what other numbers will that number be divisible?
- Find the number of all three digit natural numbers which are divisible by 9.
- Find the number of numbers which are divisible by $7$ between $100$ and $1000$.
- Elements of an array that are not divisible by any element of another array in C++
- Count the numbers divisible by ‘M’ in a given range in C++
- Find the sum of the integers between 100 and 200 that are not divisible by 9
- Find the smallest square number that is divisible by each of the numbers 5, 15 and 45.
- Find the least number that is divisible by all the numbers between 1 and 10 (both inclusive).
- Golang Program to Find the Numbers which are Divisible by 7 and Multiple of 5 in a Given Range
- Check if any large number is divisible by 17 or not in Python