Count total divisors of A or B in a given range | Utility function to find GCD of two numbers ; Utility function to find LCM of two numbers ; Function to calculate all divisors in given range ; Find LCM of a and b ; Find common divisor by using LCM ; Driver Code
function GCD ( num1 , num2 ) { let Remainder ; while ( num2 != 0 ) { Remainder = num1 % num2 ; num1 = num2 ; num2 = Remainder ; } return num1 ; } function FindLCM ( a , b ) { return parseInt ( ( a * b ) / GCD ( a , b ) , 10 ) ; } function rangeDivisor ( m , n , a , b ) { let lcm = FindLCM ( a , b ) ; let a_divisor = parseInt ( n / a , 10 ) - parseInt ( ( m - 1 ) / a , 10 ) ; let b_divisor = parseInt ( n / b , 10 ) - parseInt ( ( m - 1 ) / b , 10 ) ; let common_divisor = parseInt ( n / lcm , 10 ) - parseInt ( ( m - 1 ) / lcm , 10 ) ; let ans = a_divisor + b_divisor - common_divisor ; return ans ; } let m = 3 , n = 11 , a = 2 , b = 3 ; document . write ( rangeDivisor ( m , n , a , b ) + " " ) ; m = 11 ; n = 1000000 ; a = 6 ; b = 35 ; document . write ( rangeDivisor ( m , n , a , b ) ) ;