Chris Angelico wrote: > On Thu, Aug 4, 2011 at 4:01 AM, Steven D'Aprano > <steve+comp.lang.pyt...@pearwood.info> wrote: >> a, b = divmod(n, i) >> if b == 0: >> total += a+i >> > > Wouldn't this fail on squares? It happens to give correct results as > far as I've checked; no square up to 10,000 is called perfect, and > there are no perfect squares in that range, but I think it's > technically using an incorrect intermediate result.
Yes, you're correct -- it counts sqrt(n) twice as a factor if n is a perfect square. The obvious fix is to change the increment to: total += a if a==i else a+i I don't believe it actually makes a difference for perfect numbers, but it's worth getting right. -- Steven -- http://mail.python.org/mailman/listinfo/python-list