Maximize profit when divisibility by two numbers have associated profits | JavaScript implementation of the approach ; Function to return the maximum profit ; min ( x , y ) * n / lcm ( a , b ) ; Driver code
function __gcd ( a , b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } function maxProfit ( n , a , b , x , y ) { let res = x * Math . floor ( n / a ) ; res += y * Math . floor ( n / b ) ; res -= Math . min ( x , y ) * ( n / ( ( a * b ) / __gcd ( a , b ) ) ) ; return res ; } let n = 6 , a = 6 , b = 2 , x = 8 , y = 2 ; document . write ( maxProfit ( n , a , b , x , y ) ) ;