Print all ways to sum n integers so that they total a given sum.

Posted by noghead on Stack Overflow See other posts from Stack Overflow or by noghead
Published on 2010-04-07T15:35:15Z Indexed on 2010/04/07 17:13 UTC
Read the original article Hit count: 134

Filed under:
|

Im trying to come up with an algorithm that will print out all possible ways to sum N integers so that they total a given value.

Example. Print all ways to sum 4 integers so that they sum up to be 5.

Result should be something like:

5 0 0 0

4 1 0 0

3 2 0 0

3 1 1 0

2 3 0 0

2 2 1 0

2 1 2 0

2 1 1 1

1 4 0 0

1 3 1 0

1 2 2 0

1 2 1 1

1 1 3 0

1 1 2 1

1 1 1 2

© Stack Overflow or respective owner

Related posts about algorithm

Related posts about math