On Sun, Nov 7, 2010 at 15:53, Hugo Arts <hugo.yo...@gmail.com> wrote: > On Mon, Nov 8, 2010 at 12:36 AM, Richard D. Moores <rdmoo...@gmail.com> wrote: >> 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. >> > > You can't do that inside a list comprehension. Either get rid of the > comprehension and do a regular loop, or get rid of the n/x expression. > > I'd suggest replacing the whole check with x % n == 0.
Wow! using n % x == 0 is about 3x faster than what I had before. > n is a proper > divisor of x if there is no remainder after division, after all. This > also means you won't have to do a cast, which tend to be fairly > expensive. I don't know what a cast is. Would that be the int(n/x)? Here are 2 versions, of about equal speed: 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 n % x == 0]) def proper_divisors(n): sum_ = 0 for x in range(1,n): if n % x == 0: sum_ += x return sum_ > On another note, getting rid of the list comprehension and using a > generator expression will be even faster, since you won't have to > build the list. Could you spell that out for me? Thanks, Dick _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor