Smallest value of N such that the sum of all natural numbers from K to N is at least X | Function to find the minimum possible value of N such that sum of natural numbers from K to N is at least X ; If K is greater than X ; Stores value of minimum N ; Stores the sum of values over the range [ K , ans ] ; Iterate over the range [ K , N ] ; Check if sum of first i natural numbers is >= X ; Print the possible value of ans ; Driver Code
function minimumNumber ( K , X ) { if ( K > X ) { document . write ( " " ) ; return ; } let ans = 0 ; let sum = 0 ; for ( let i = K ; i <= X ; i ++ ) { sum += i ; if ( sum >= X ) { ans = i ; break ; } } document . write ( ans ) ; } let K = 5 , X = 13 ; minimumNumber ( K , X ) ;