I have to create a method that takes two integers, let them be n
and m
, and returns how many ways there are to sum m
positive numbers
Since you know how many digits you are to use I believe you can do this.
First of you can do this by repeatedly doing partition(n, 2). If you want n = 3, m = 3 you can just do partition(n, 2), and then for each of the answers you do partition(k, 2).
Example:
partition(6, 3):
partition(6, 2):
5 + 1, 4 + 2, 3 + 3, 2 + 4, 5 + 1
partition(5, 2):
4 + 1, 3 + 2 ...
Then you just add all of them together:
(4+1) + 1, (3+2)+1, (2+3)+1, (1+4)+1, (3+1)+2...
and sort them (largest number first).
4+1+1, 4+1+1...
Then you can just remove all the duplicates
Partition(n, 2) will run in O(n) (since you just need to loop up to n and do x + (n-x)). The number of times you will have to do this is O(m) of some kind. And sorting can be done in n (since you know it's all whole numbers). So I think this will run in O(n*m), which isn't good but it might be usable for you (if n or m is reasonably small).