# Count integers in the range [A, B] that are not divisible by C and D

Given four integers **A, B, C** and **D**. The task is to find the count of integers in the range **[A, B]** that are not divisible by **C** and **D** .

**Examples:**

Input:A = 4, B = 9, C = 2, D = 3

Output:2

5 and 7 are such integers.

Input:A = 10, B = 50, C = 4, D = 6

Output:28

**Approach:** First include all the integers in the range in the required answer i.e. **B – A + 1**. Then remove all the numbers which are divisible by **C** and **D** and finally add all the numbers which are divisible by both **C** and **D**.

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 ` `// integers from the range [a, b] that ` `// are not divisible by c and d ` `int` `countNums(` `int` `a, ` `int` `b, ` `int` `c, ` `int` `d) ` `{ ` ` ` `// Numbers which are divisible by c ` ` ` `int` `x = b / c - (a - 1) / c; ` ` ` ` ` `// Numbers which are divisible by d ` ` ` `int` `y = b / d - (a - 1) / d; ` ` ` ` ` `// Find lowest common factor of c and d ` ` ` `int` `k = (c * d) / __gcd(c, d); ` ` ` ` ` `// Numbers which are divisible by both c and d ` ` ` `int` `z = b / k - (a - 1) / k; ` ` ` ` ` `// Return the required answer ` ` ` `return` `b - a + 1 - x - y + z; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a = 10, b = 50, c = 4, d = 6; ` ` ` ` ` `cout << countNums(a, b, c, d); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to return the count of ` `// integers from the range [a, b] that ` `// are not divisible by c and d ` `static` `int` `countNums(` `int` `a, ` `int` `b, ` `int` `c, ` `int` `d) ` `{ ` ` ` `// Numbers which are divisible by c ` ` ` `int` `x = b / c - (a - ` `1` `) / c; ` ` ` ` ` `// Numbers which are divisible by d ` ` ` `int` `y = b / d - (a - ` `1` `) / d; ` ` ` ` ` `// Find lowest common factor of c and d ` ` ` `int` `k = (c * d) / __gcd(c, d); ` ` ` ` ` `// Numbers which are divisible by both c and d ` ` ` `int` `z = b / k - (a - ` `1` `) / k; ` ` ` ` ` `// Return the required answer ` ` ` `return` `b - a + ` `1` `- x - y + z; ` `} ` `static` `int` `__gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `(b == ` `0` `) ` ` ` `return` `a; ` ` ` `return` `__gcd(b, a % b); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String []args) ` `{ ` ` ` `int` `a = ` `10` `, b = ` `50` `, c = ` `4` `, d = ` `6` `; ` ` ` ` ` `System.out.println(countNums(a, b, c, d)); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `from` `math ` `import` `gcd ` ` ` `# Function to return the count of ` `# integers from the range [a, b] that ` `# are not divisible by c and d ` `def` `countNums(a, b, c, d) : ` ` ` ` ` `# Numbers which are divisible by c ` ` ` `x ` `=` `b ` `/` `/` `c ` `-` `(a ` `-` `1` `) ` `/` `/` `c; ` ` ` ` ` `# Numbers which are divisible by d ` ` ` `y ` `=` `b ` `/` `/` `d ` `-` `(a ` `-` `1` `) ` `/` `/` `d; ` ` ` ` ` `# Find lowest common factor of c and d ` ` ` `k ` `=` `(c ` `*` `d) ` `/` `/` `gcd(c, d); ` ` ` ` ` `# Numbers which are divisible ` ` ` `# by both c and d ` ` ` `z ` `=` `b ` `/` `/` `k ` `-` `(a ` `-` `1` `) ` `/` `/` `k; ` ` ` ` ` `# Return the required answer ` ` ` `return` `(b ` `-` `a ` `+` `1` `-` `x ` `-` `y ` `+` `z); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `a ` `=` `10` `; b ` `=` `50` `; c ` `=` `4` `; d ` `=` `6` `; ` ` ` ` ` `print` `(countNums(a, b, c, d)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the count of ` `// integers from the range [a, b] that ` `// are not divisible by c and d ` `static` `int` `countNums(` `int` `a, ` `int` `b, ` `int` `c, ` `int` `d) ` `{ ` ` ` `// Numbers which are divisible by c ` ` ` `int` `x = b / c - (a - 1) / c; ` ` ` ` ` `// Numbers which are divisible by d ` ` ` `int` `y = b / d - (a - 1) / d; ` ` ` ` ` `// Find lowest common factor of c and d ` ` ` `int` `k = (c * d) / __gcd(c, d); ` ` ` ` ` `// Numbers which are divisible by both c and d ` ` ` `int` `z = b / k - (a - 1) / k; ` ` ` ` ` `// Return the required answer ` ` ` `return` `b - a + 1 - x - y + z; ` `} ` ` ` `static` `int` `__gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `(b == 0) ` ` ` `return` `a; ` ` ` `return` `__gcd(b, a % b); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String []args) ` `{ ` ` ` `int` `a = 10, b = 50, c = 4, d = 6; ` ` ` ` ` `Console.WriteLine(countNums(a, b, c, d)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

**Output:**

28

## Recommended Posts:

- Count integers in a range which are divisible by their euler totient value
- Ways to form an array having integers in given range such that total sum is divisible by 2
- Count of m digit integers that are divisible by an integer n
- Count of integers of the form (2^x * 3^y) in the range [L, R]
- 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 of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Minimum elements to be added in a range so that count of elements is divisible by K
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Sum of last digit of all integers from 1 to N divisible by M
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i]
- Number of substrings divisible by 6 in a string of integers
- Number of substrings divisible by 4 in a string of integers

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.