Count integers in the range [ A , B ] that are not divisible by C and D | C ++ implementation of the approach ; Function to return the count of integers from the range [ a , b ] that are not divisible by c and d ; Numbers which are divisible by c ; Numbers which are divisible by d ; Find lowest common factor of c and d ; Numbers which are divisible by both c and d ; Return the required answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countNums ( int a , int b , int c , int d ) { int x = b / c - ( a - 1 ) / c ; int y = b / d - ( a - 1 ) / d ; int k = ( c * d ) / __gcd ( c , d ) ; int z = b / k - ( a - 1 ) / k ; return b - a + 1 - x - y + z ; } int main ( ) { int a = 10 , b = 50 , c = 4 , d = 6 ; cout << countNums ( a , b , c , d ) ; return 0 ; }