Advertisement
jules0707

different_summands

Jun 30th, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.61 KB | None | 0 0
  1. # Uses python3
  2. import sys
  3.  
  4.  
  5. def optimal_summands(n):
  6.     summands = []
  7.     # write your code here
  8.     k = n
  9.     l = 1
  10.     # straight forward implementation of the hint
  11.     # we could also do a recursive call but the grader gives me an wrong format index out of bond exception
  12.     while k > 2 * l:
  13.         summands.append(l)
  14.         k -= l
  15.         l += 1
  16.     summands.append(k)
  17.  
  18.     return summands
  19.  
  20.  
  21. if __name__ == '__main__':
  22.     user_input = sys.stdin.read()
  23.     n = int(user_input)
  24.     summands = optimal_summands(n)
  25.     print(len(summands))
  26.     for x in summands:
  27.         print(x, end=' ')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement