When faced with a similar problem, it is often a good idea to take a step back from your editor / IDE and think about the problem by pulling a simple case on the board. Don't even make pseudo-code, just pull out a flowchart of how a simple case (e.g. a = 3 ) for this problem will be the turtle all the way down. Also, don't worry about duplicate combinations at first. Try to find a solution that will give you all the necessary combinations, and then improve your solution so as not to give you duplicates. In this case, why not look at the controlled case of a = 3 ? Let me take a small picture for you. A green checkmark means that we have reached a valid combination, a red cross means that the combination is invalid.

As you can see, we start with three empty subcombinations, and then create three new subcombinations, adding a number to each of them. We want to study all the possible ways, so we choose 1, 2 and 3 and get [1] , [2] and [3] . If the sum of the numbers in the combination is 3, we have found a valid combination, so we can stop to study this way. If the sum of the numbers in the combination exceeds 3, the combination is invalid and we can also stop. If this is not the case, we simply continue to create combinations until we get some valid or invalid solution.
Since your question, apparently, is mainly about how to develop a recursive solution for such problems and less about the specific syntax, and you just managed to find the solution in C ++ that I am going to provide in Python (it almost looks like a pseudo- code, and this is what he knows).
def getcombs(a, combo = None):
Check the code!
>>> combos = getcombs(3) >>> for combo in combos: print(combo) ... [1, 1, 1] [1, 2] [2, 1] [3]
This seems to be fine, another test for a = 5 :
>>> combos = getcombs(5) >>> for combo in combos: print(combo) ... [1, 1, 1, 1, 1] [1, 1, 1, 2] [1, 1, 2, 1] [1, 1, 3] [1, 2, 1, 1] [1, 2, 2] [1, 3, 1] [1, 4] [2, 1, 1, 1] [2, 1, 2] [2, 2, 1] [2, 3] [3, 1, 1] [3, 2] [4, 1] [5]
The solution includes all seven combinations that we were looking for, but the code still produces duplicates. As you may have noticed, there is no need to take a number smaller than the previous selected number to create all the combinations. So add code that is just starting to build extcombo for numbers that are not less than the last current number in the combination. If the combination is empty, we simply set the previous number to 1.
def getcombs(a, combo = None):
Check the test code again!
>>> combo = getcombs(5) >>> for combo in combos: print(combo) ... [1, 1, 1, 1, 1] [1, 1, 1, 2] [1, 1, 3] [1, 2, 2] [1, 4] [2, 3] [5]
The presented solution may not be the most effective that exists, but I hope it will encourage you to think recursively. Break the problem down step by step, pull out a simple case for small inputs, and solve one problem at a time.