My Code for this problem in Python.
def optimal_summands(n):
if n <= 2:
return [n]
summands = []
remainder = n
j = 1
while j <= remainder:
remainder -= j
if remainder <= j:
summands.append(remainder + j)
break
summands.append(j)
j += 1
return summands
if __name__ == '__main__':
n = int(input())
summands = optimal_summands(n)
print(len(summands))
print(*summands)
> Please try this solution, much simpler
def optimal_summands(n):
if n <= 2:
return [n]
summands = []
remainder = n
j = 1
while j <= remainder:
remainder -= j
if remainder <= j:
summands.append(remainder + j)
break
summands.append(j)
j += 1
return summands
if __name__ == '__main__':
n = int(input())
summands = optimal_summands(n)
print(len(summands))
print(*summands)