diff options
author | Santosh G. Vattam | 2010-03-11 18:01:23 +0530 |
---|---|---|
committer | Santosh G. Vattam | 2010-03-11 18:01:23 +0530 |
commit | e165b2bf43110b3faf24af89fa479afcac6dad25 (patch) | |
tree | 671a499f31425f35ea158136da94f7a5272749d8 /day1 | |
parent | b12e0358be2b89b5b0545c72977c1c25b4daa8a9 (diff) | |
download | workshops-e165b2bf43110b3faf24af89fa479afcac6dad25.tar.gz workshops-e165b2bf43110b3faf24af89fa479afcac6dad25.tar.bz2 workshops-e165b2bf43110b3faf24af89fa479afcac6dad25.zip |
Updated after Day 2 at GRDCS
Diffstat (limited to 'day1')
-rw-r--r-- | day1/exercise/aliquot.py | 6 | ||||
-rw-r--r-- | day1/exercise/amicable.py | 9 | ||||
-rw-r--r-- | day1/exercise/amicable_debug.py | 14 | ||||
-rw-r--r-- | day1/exercise/even_perfect_4a.py | 2 | ||||
-rw-r--r-- | day1/exercise/kwfreq.py | 3 | ||||
-rw-r--r-- | day1/exercise/pytriads.py | 4 | ||||
-rw-r--r-- | day1/exercise/word_frequencies.py | 8 |
7 files changed, 22 insertions, 24 deletions
diff --git a/day1/exercise/aliquot.py b/day1/exercise/aliquot.py index 25518f0..7d5676c 100644 --- a/day1/exercise/aliquot.py +++ b/day1/exercise/aliquot.py @@ -1,5 +1,7 @@ - def aliquot(n): + """returns the aliquot of a number which + is defined as the sum of all the proper + divisors of a number""" sum = 1 i = 2 @@ -7,7 +9,7 @@ def aliquot(n): if n % i == 0: sum += i + (n / i) i += 1 - if n % i == 0: sum += i + if i*i == n: sum += i return sum n = int(raw_input('Enter a number? ')) diff --git a/day1/exercise/amicable.py b/day1/exercise/amicable.py index c9aea71..6e5b2e1 100644 --- a/day1/exercise/amicable.py +++ b/day1/exercise/amicable.py @@ -1,15 +1,8 @@ -def is_perfect_square(n): - i = 1 - while i * i < n: - i += 1 - return i * i == n, i - def aliquot(n): sum = 1 i = 2 - is_ps, root = is_perfect_square(n) - while i < root: + while i * i < n: if n % i == 0: sum += i + (n / i) i += 1 diff --git a/day1/exercise/amicable_debug.py b/day1/exercise/amicable_debug.py index 9369219..6f42d86 100644 --- a/day1/exercise/amicable_debug.py +++ b/day1/exercise/amicable_debug.py @@ -1,16 +1,20 @@ import math def aliquot(n): - sum = 0 - for i in range(1, math.sqrt(n)+1): + sum = 1 + i = 2 + + while i * i < n: if n % i == 0: - sum += i + n/i + sum += i + (n / i) + i += 1 + if i*i == n: sum += i return sum amicable = [] -for n in range(10000, 100000): +for n in range(1000, 10000): m = aliquot(n) - if aliquot(m) == n: + if m > n and aliquot(m) == n: amicable.append((m, n)) print amicable diff --git a/day1/exercise/even_perfect_4a.py b/day1/exercise/even_perfect_4a.py index ccadc1c..d11c44d 100644 --- a/day1/exercise/even_perfect_4a.py +++ b/day1/exercise/even_perfect_4a.py @@ -11,4 +11,4 @@ while i <= 94: square = i * i if all_digits_even(square): print square - i += 1 + i += 2 diff --git a/day1/exercise/kwfreq.py b/day1/exercise/kwfreq.py index e905e26..1414c4b 100644 --- a/day1/exercise/kwfreq.py +++ b/day1/exercise/kwfreq.py @@ -1,6 +1,3 @@ -import keyword -f = open('amicable.py') - freq = {} for line in f: words = line.split() diff --git a/day1/exercise/pytriads.py b/day1/exercise/pytriads.py index bea543d..caf9137 100644 --- a/day1/exercise/pytriads.py +++ b/day1/exercise/pytriads.py @@ -10,8 +10,8 @@ def gcd(a, b): else: return gcd(b, a%b) -for a in range(3, 501): - for b in range( a+1, 501, 2): +for a in range(3, 101): + for b in range( a+1, 101, 2): if gcd( a, b ) == 1: is_ps, c = is_perfect_square((a * a) + (b * b)) if is_ps: print a, b, c diff --git a/day1/exercise/word_frequencies.py b/day1/exercise/word_frequencies.py index deb9802..1f7351c 100644 --- a/day1/exercise/word_frequencies.py +++ b/day1/exercise/word_frequencies.py @@ -1,11 +1,13 @@ -f = open('/home/madhu/pyprogs/pytriads.py') +f = open('/home/vattam/Desktop/circulate/word-freq/holmes.txt') freq = {} for line in f: words = line.split() for word in words: key = word.strip(',.!;?\'" ') - value = freq.get(key, 1) - freq[key] = value + 1 + if key in freq: + freq[key] += 1 + else: + freq[key] = 1 print freq |