Count of quadruplets from range [ L , R ] having GCD equal to K | Function to return the gcd of a and b ; Function to return the count of quadruplets having gcd = k ; To store the required count ; Check every quadruplet pair whether its gcd is k ; Return the required count ; Driver code
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function countQuadruplets ( l , r , k ) { var count = 0 ; for ( u = l ; u <= r ; u ++ ) { for ( v = l ; v <= r ; v ++ ) { for ( w = l ; w <= r ; w ++ ) { for ( x = l ; x <= r ; x ++ ) { if ( gcd ( gcd ( u , v ) , gcd ( w , x ) ) == k ) count ++ ; } } } } return count ; } var l = 1 , r = 10 , k = 2 ; document . write ( countQuadruplets ( l , r , k ) ) ;