[Tutor] Recursive Least Common Multiple
Sean 'Shaleh' Perry
shalehperry@home.com
Tue, 17 Jul 2001 21:42:50 -0700 (PDT)
def adder(n):
val = []
count = 1
dummy = n
while((count < dummy) and ((2 * count) <= n)):
dummy = dummy - 1
val.append([count, dummy])
count = count + 1
return val
> 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.
>
This solves the 2 operands case. The (2 * count) <= n helps when n is large,
although this still results in a minimal list without it.
$ /tmp/adder.py
[[1, 29], [2, 28], [3, 27], [4, 26], [5, 25], [6, 24], [7, 23], [8, 22], [9,
21], [10, 20], [11, 19], [12, 18], [13, 17], [14, 16], [15, 15]]
I swear there is an algorithm that solves this problem. The loop construct
just feels like the wrong approach.
So, then you want [1,1,1,1,...1]. Well, the value of n represents the largest
list you will have (i.e. if n == 30, the last list will have len(list) == 30).
Call adder for the right hand value until you end up with that list.