-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdifferent_summands.py
More file actions
46 lines (35 loc) · 922 Bytes
/
different_summands.py
File metadata and controls
46 lines (35 loc) · 922 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Maximum Number of Prizes (Different Summands)
# Author: jerrybelmonte
# Input: Single integer n.
# Output: First line is the maximum number k of integers.
# Followed by the distinct integers that sum up to n.
def optimal_summands(n: int):
"""
Gets the maximum number of distinct prizes.
>>> optimal_summands(6)
[1, 2, 3]
>>> optimal_summands(8)
[1, 2, 5]
>>> optimal_summands(2)
[2]
"""
if n <= 2:
return [n]
opt_sum = 0
summands = []
for num in range(1, n):
opt_sum += num
if opt_sum < n:
summands.append(num)
elif opt_sum == n:
summands.append(num)
break
else:
summands[-1] += n - (opt_sum - num)
break
return summands
if __name__ == '__main__':
res = optimal_summands(int(input()))
print(len(res))
for x in res:
print(x, end=' ')