title: Amicable Numbers
body: <p>I am struggling with optimizing these functions that I have used to calculate the sum of the amicable pairs under 10000. An <a href="http://mathworld.wolfram.com/AmicablePair.html" rel="nofollow">amicable pair</a> is a pair (a, b) where the sum of the divisors of "a" excluding "a" itself equals b and the sum of the divisors of "b" excluding "b" itself equals "a".</p>
  
  <p>I.e. divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110: The sum of which is 284. And the sum of the divisors of 284 (1, 2, 4, 71 and 142) equals 220. </p>
  
  <p>My code is:</p>
  
  <pre><code>import math
  def Divisorsbaritself(x):
      divList = [1]
      y = 2
      while y &lt;= math.sqrt(x):
          if x % y == 0:
              divList.append(y)
              divList.append(int(x / y))
          y += 1
      return sum(divList)
  
  def amicable():
      solution = []
      for i in range(10000):
          if Divisorsbaritself(Divisorsbaritself(i)) == i:
              solution.append(i)
      return sum(solution)
  
  print amicable()
  </code></pre>
  
  <p>I need help with understanding why the amicable function is not working. To me it makes logical sense that the <code>if Divisorsbaritself(Divisorsbaritself(i)) == i:</code> condition is the right condition to include <code>i</code> in the list, but it is giving me 40285, rather than 31626, the answer. </p>