While12. An integer N (> 1) is given. Output the largest integer K for which the sum 1 + 2 + ... + K is less than or equal to N, and the sum itself.

Solution in Python 3:

import random

N = random.randrange(2,200)
#N = 91
print('N = ', N)

K = 1
S = 1
while S <= N:
K += 1
S += K
print("K = {0}, S = {1}".format(K,S))

S -= K
K -= 1
print()
print("K = {0}, S = {1}".format(K,S))