I'm trying to implement a simple program that aims to solve the subset problem in python. 
I've found the following code that involves dynamically programming:

```
def subset_sum(numbers, target, partial=[]):
    s = sum(partial)

    # check if the partial sum is equals to target
    if s == target: 
        print("sum(%s)=%s" % (partial, target))
    if s >= target:
        return  # if we reach the number why bother to continue

    for i in range(len(numbers)):
        n = numbers[i]
        remaining = numbers[i+1:]
        subset_sum(remaining, target, partial + [n]) 

```

The code works but it finds all the possible combinations, while I'm only interested on subsets with a maximum number of addends that I want to specify time to time. 

In other words I'd like to set a maximum number of the internal loops. To do that I've written the following code that considers all the possible combinations of at most 4 numbers that sum to the target (i.e. 3 internal loops)

```
def subset_sum(n_list, tot):

    cnd = n_list[n_list < tot]
    s = np.sort(cnd)
    n_max = len(cnd)

    possibilities = []

    for i1 in range(n_max):
        i2 = i1+1

        while (i2<n_max)and(s[i1]+s[i2]<=tot):
            if (s[i1]+s[i2]==tot):
                possibilities.append([s[i1],s[i2]])
            i3 = i2+1

            while (i3<n_max)and(s[i1]+s[i2]+s[i3]<=tot):
                if (s[i1]+s[i2]+s[i3]==tot):
                    possibilities.append([s[i1],s[i2],s[i3]])
                i4 = i3+1

                while (i4<n_max)and(s[i1]+s[i2]+s[i3]+s[i4]<=tot):
                    if (s[i1]+s[i2]+s[i3]+s[i4]==tot):
                        possibilities.append([s[i1],s[i2],s[i3],s[i4]])

                    i4+=1
                i3+=1
            i2+=1 
    return possibilities

```

This code works pretty well, can also be speed up with numba (while the first code no) but I cannot fix the maximum number of addends. 
Is there a way to implement the function subset\_sum with an extra argument that fix the maximum number of addends that sum to the target?