Find count of numbers from 0 to n which satisfies the given equation for a value K | Function to find the values ; Calculate the LCM ; Calculate the multiples of lcm ; Find the values which satisfies the given condition ; Subtract the extra values ; Return the final result ; Driver code
function findAns ( a , b , n ) { var lcm = ( a * b ) / __gcd ( a , b ) ; var multiples = ( n / lcm ) + 1 ; var answer = Math . max ( a , b ) * multiples ; var lastvalue = lcm * ( n / lcm ) + Math . max ( a , b ) ; if ( lastvalue > n ) answer = answer - ( lastvalue - n - 1 ) ; return answer ; } function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } var a = 1 , b = 13 , n = 500 ; document . write ( findAns ( a , b , n ) + " " ) ;