math - need divisors algorithm
Peter Schorn
peter.schorn at acm.org
Thu Apr 7 15:12:40 EDT 2005
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
More information about the Python-list
mailing list