Menu

#8 prime.Divisors(n)

open
nobody
5
2012-01-31
2012-01-31
No

I propose to split the prime.properDivisors function in order to add a fast (no sort) Divisors function:

def Divisors(n):
"""
Return all divisors of n (including 1 and n).
"""
if n in (2, 3, 5, 7, 11, 13, 17, 19, 23):
return []
else:
l = [1]
for p, e in _factor(n):
for j in range(1, e + 1):
l += [k*pow(p, j) for k in l if k % p != 0]

def properDivisors(n):
"""
Return proper divisors of n (divisors of n excluding 1 and n).

It is only useful for a product of small primes.
One can use FactoredInteger.proper_divisors() as well.
"""
l=Divisors(n)
l.remove(1)
if n!=1:l.remove(n) # prevents properDivisors(1) from crashing
l.sort()
return l

Discussion

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.