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
def minimumNumber ( K , X ) : NEW_LINE INDENT if ( K > X ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT ans = 0 NEW_LINE sum = 0 NEW_LINE for i in range ( K , X + 1 ) : NEW_LINE INDENT sum += i NEW_LINE if ( sum >= X ) : NEW_LINE INDENT ans = i NEW_LINE break NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT K = 5 NEW_LINE X = 13 NEW_LINE minimumNumber ( K , X ) NEW_LINE