Cake Distribution Problem | Function to return the remaining count of cakes ; Sum for 1 cycle ; no . of full cycle and remainder ; Driver Code
function cntCakes ( n , m ) { let sum = ( n * ( n + 1 ) ) / 2 ; let quo = m / sum ; let rem = m % sum ; let ans = m - quo * sum + 6 ; let x = ( - 1 + Math . pow ( ( 8 * rem ) + 1 , 0.5 ) ) ; ans = ans - x * ( x + 1 ) / 2 ; return parseInt ( ans , 10 ) ; } let n = 3 ; let m = 8 ; let ans = cntCakes ( n , m ) ; document . write ( ans ) ;