day1/exercise/amicable.py
changeset 94 8c92864c184b
parent 64 333092b68926
child 380 669b72283b55
--- a/day1/exercise/amicable.py	Fri Oct 09 22:57:00 2009 +0530
+++ b/day1/exercise/amicable.py	Sun Oct 11 16:51:43 2009 +0530
@@ -1,18 +1,25 @@
-import math
+def is_perfect_square(n):
+    i = 1
+    while i * i < n:
+        i += 1
+    return i * i == n, i
 
 def aliquot(n):
-    sum = 0
-    for i in range(1, int(math.sqrt(n))+1):
+    sum = 1
+    i = 2
+
+    is_ps, root = is_perfect_square(n)
+    while i < root:
         if n % i == 0:
-            sum += i + n/i
+            sum += i + (n / i)
+        i += 1
     return sum
 
 amicable = []
-for n in range(10000, 100000):
+
+n = 1000
+while n < 10000:
     m = aliquot(n)
-    if aliquot(m) == n:
-        amicable.append((m, n))
-
-print amicable
-
-# please please please profile this.
+    if m > n and aliquot(m) == n:
+        print m, n
+    n += 1