I have modified a recursive function designed to print all the integer partitions of a positive integer to create a function that returns all partitions of the "number" parameter if they include a number contained in the "interesting" parameter. I would like to use this for very large numbers (10-50 million), but am getting errors referring to the recursion depth. My question is whether there is a way to do this more efficiently by recursion, and if not, whether there is another way to do this.
def partition(number, interesting): # returns all the partitions of number that are included in the interesting list
answer = set()
if number in interesting:
answer.add((number, ))
for x in range(1, number):
if x in interesting:
for y in partition(number - x, interesting):
answer.add(tuple(sorted((x, ) + y)))
return answer
Set[Tuple[int,...]], but do you need to return the entire set at once, or could you use return the set elements one at a time using a generator? Is this a programming challenge? If so post a link to the problem, as well as the full problem description in the question, including limits, such as the number of interesting numbers, as well as their ranges. \$\endgroup\$