summaryrefslogtreecommitdiff
path: root/day1/exercise/amicable.py
diff options
context:
space:
mode:
authorMadhusudan.C.S2009-10-11 16:51:43 +0530
committerMadhusudan.C.S2009-10-11 16:51:43 +0530
commit90327591260622d4569b2dfa860d019109301288 (patch)
tree7e3249f19681f4ee69d3f6443445d597f16aaa92 /day1/exercise/amicable.py
parent7effa13d98dcef70307bf56490329f514f82b60a (diff)
downloadworkshops-more-scipy-90327591260622d4569b2dfa860d019109301288.tar.gz
workshops-more-scipy-90327591260622d4569b2dfa860d019109301288.tar.bz2
workshops-more-scipy-90327591260622d4569b2dfa860d019109301288.zip
Almost last set of official solutions and final quiz.
Diffstat (limited to 'day1/exercise/amicable.py')
-rw-r--r--day1/exercise/amicable.py29
1 files changed, 18 insertions, 11 deletions
diff --git a/day1/exercise/amicable.py b/day1/exercise/amicable.py
index 0986e33..c9aea71 100644
--- a/day1/exercise/amicable.py
+++ b/day1/exercise/amicable.py
@@ -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):
- m = aliquot(n)
- if aliquot(m) == n:
- amicable.append((m, n))
-print amicable
-
-# please please please profile this.
+n = 1000
+while n < 10000:
+ m = aliquot(n)
+ if m > n and aliquot(m) == n:
+ print m, n
+ n += 1