Count integers in the range [ A , B ] that are not divisible by C and D | 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
function countNums ( a , b , c , d ) { let x = parseInt ( b / c ) - parseInt ( ( a - 1 ) / c ) ; let y = parseInt ( b / d ) - parseInt ( ( a - 1 ) / d ) ; let k = parseInt ( ( c * d ) / __gcd ( c , d ) ) ; let z = parseInt ( b / k ) - parseInt ( ( a - 1 ) / k ) ; return b - a + 1 - x - y + z ; } function __gcd ( a , b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } let a = 10 , b = 50 , c = 4 , d = 6 ; document . write ( countNums ( a , b , c , d ) ) ;