Philp Smith wrote:
Hi

Does anyone have suggested code for a compact, efficient, elegant, most of all pythonic routine to produce a list of all the proper divisors of an integer (given a list of prime factors/powers)



What about

# Returns a list of all divisors of n = a1^e1*a2^e2* ... *an^en where
# input parameter l = [(a1, e1), (a2, e2), ..., (an, en)]
def divisors(l):
    if  l: return [i*j for i in [l[0][0]**k for k in range(l[0][1] + 1)]
      for j in divisors(l[1:])]
    else: return [1]


# divisors([(2,3),(3,2)]) == [1, 3, 9, 2, 6, 18, 4, 12, 36, 8, 24, 72]


Regards, Peter -- http://mail.python.org/mailman/listinfo/python-list

Reply via email to