[Tutor] List comprehension question

C or L Smith smiles at worksmail.net
Wed Nov 10 03:29:58 CET 2010


>>   1. Re: List comprehension question (Richard D. Moores)
>>> ?: def proper_divisors_sum(n):

>>> A few questions--noting, of course, that I'm not reading this with
>>> an eye toward performance, which it seems you are, but these occur
>>> to me:

Tim Peters had a beautiful little version of divisors at

http://stackoverflow.com/questions/1010381/python-factorization

A modified version of this is part of the python CAS system sympy (sympy.org). 
>From sympy you simply do:

>>> from sympy import divisors
>>> list(divisors(256))
[1, 2, 4, 8, 16, 32, 64, 128, 256]

So your proper divisors would just be sum(divisors(n)) - n.
The divisors function there is in the ntheory/factor_.py file.

/c







More information about the Tutor mailing list