Count pairs ( i , j ) such that ( i + j ) is divisible by A and B both | Javascript implementation of above approach ; Function to find the LCM ; Function to count the pairs ; Driver code
function gcd ( a , b ) { return b == 0 ? a : gcd ( b , a % b ) ; } function find_LCM ( x , y ) { return Math . floor ( ( x * y ) / gcd ( x , y ) ) ; } function CountPairs ( n , m , A , B ) { let cnt = 0 ; let lcm = find_LCM ( A , B ) ; for ( let i = 1 ; i <= n ; i ++ ) cnt += Math . floor ( ( m + ( i % lcm ) ) / lcm ) ; return cnt ; } let n = 60 , m = 90 , A = 5 , B = 10 ; document . write ( CountPairs ( n , m , A , B ) ) ;