[Tutor] Recursive Least Common Multiple
Timothy M. Brauch
tbrauch@mindless.com
Tue, 17 Jul 2001 23:58:26 -0400
I would like to break up a number into its addition parts. For example,
with the number 7, I need a list returned like:
[[1,6],[2,5],[3,4],[4,3],[5,2],[6,1],[1,1,5],[1,2,4],[1,3,3],[1,4,2],[1,5,1],...[2,1,1,1,1,1],[1,1,1,1,1,1,1]]
Ideally, I can make this list smaller by sorting the individual lists
and removing duplicates. That is, I only need [1,6] not [6,1] and [2,5]
not [5,2] and [1,1,1,1,1,2] not [2,1,1,1,1,1]. That part shouldn't be
hard. And speed is not a concern right now (it might be if I start
using very large numbers, but I should be using mostly small numbers).
I tried a small program that can break it into the groups of two
numbers... I just can't seem to figure out how to get it to go on to 3
numbers, 4 numbers, 5 numbers, and beyond.
------------------------------------------
def adder(n):
count=0
dummy=n
list=[]
while (count<n):
new_item=[count,dummy]
new_item.sort()
if new_item not in list:
list.append(new_item)
count=count+1
dummy=dummy-1
return list
--------------------------------------------
I really feel like my code could be a lot shorter if I knew some trick,
and some other trick would actually do what I need. Any help would be
greatly appreciated. Also, I created the dummy variable so as not to
destroy n in case I need to use it later.
- Tim