[Tutor] List comprehension question

Richard D. Moores rdmoores at gmail.com
Mon Nov 8 00:36:30 CET 2010


def proper_divisors(n):
    """
    Return the sum of the proper divisors of positive integer n
    """
    return sum([x for x in range(1,n) if int(n/x) == n/x])

The list comprehension is this function is inefficient in that it computes
n/x twice. I'd like to do an  a = n/x and use a in
"if int(a) == a", but I don't know how.

Thanks,

Dick Moores
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101107/20dbab15/attachment-0001.html>


More information about the Tutor mailing list