I found a solution in Python for this problem, but do not understand it. The problem is how many ways an integer n can be written as the sum of at least two positive integers. For example, take n = 5. The number 5 can be written as
4 + 1
3 + 2
3 + 1 + 1
2 + 2 + 1
2 + 1 + 1 + 1
1 + 1 + 1 + 1 + 1
Here's a solution for given $n$.
# zero-based array (first index is 0)
ways = [1, 0, ..., 0] (n zeroes)
for i in 1, ..., n-1:
for j in i, ..., n:
ways[j] = ways[j] + ways[j-i]
print ways[n]
This solution is elegant and efficient, but unfortunately, I do not understand the logic. Can someone please explain the logic of this solution to the problem? Is there a way to make this algorithm easy to understand?