summaryrefslogtreecommitdiff
path: root/day1/exercise
diff options
context:
space:
mode:
Diffstat (limited to 'day1/exercise')
-rw-r--r--day1/exercise/aliquot.py9
-rw-r--r--day1/exercise/collatz.py9
-rw-r--r--day1/exercise/datestring.py34
-rw-r--r--day1/exercise/pytriads.py14
4 files changed, 26 insertions, 40 deletions
diff --git a/day1/exercise/aliquot.py b/day1/exercise/aliquot.py
index 96ea916..25518f0 100644
--- a/day1/exercise/aliquot.py
+++ b/day1/exercise/aliquot.py
@@ -1,18 +1,13 @@
-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
+ if n % i == 0: sum += i
return sum
n = int(raw_input('Enter a number? '))
diff --git a/day1/exercise/collatz.py b/day1/exercise/collatz.py
index 2b980d7..7bed967 100644
--- a/day1/exercise/collatz.py
+++ b/day1/exercise/collatz.py
@@ -1,7 +1,10 @@
-a = -1
-while a > 1:
+a = int( raw_input( 'Enter number: ') )
+while a != 4:
print a
- if a % 2:
+ if a % 2 == 1:
a = a * 3 + 1
else:
a /= 2
+print 4
+print 2
+print 1
diff --git a/day1/exercise/datestring.py b/day1/exercise/datestring.py
index d990904..4e8cad4 100644
--- a/day1/exercise/datestring.py
+++ b/day1/exercise/datestring.py
@@ -1,23 +1,15 @@
-def get_date_from_str(date_str):
- month2mm = {
- 'January': 1,
- 'February': 2,
- 'March': 3,
- 'April': 4,
- 'May': 5,
- 'June': 6,
- 'July': 7,
- 'August': 8,
- 'September': 9,
- 'October': 10,
- 'November': 11,
- 'December': 12,
- }
-
- dd, month, yyyy = date_str.split()
-
- mm = month2mm[month]
- return int(yyyy), int(dd.strip(',')), mm
+month2mm = { 'JAN': 1, 'FEB': 2, 'MAR': 3, 'APR': 4, 'MAY': 5,
+'JUN': 6, 'JUL': 7, 'AUG': 8, 'SEP': 9, 'OCT': 10, 'NOV': 11,
+'DEC': 12 }
+COMMA = ','
+SPACE = ' '
date_str = raw_input('Enter a date string? ')
-print get_date_from_str(date_str)
+date_str = date_str.replace( COMMA, SPACE)
+d, m, y = date_str.split()
+dd = int( d )
+mon = m[:3].upper()
+mm = month2mm[mon]
+yyyy = int( y )
+
+print dd,mm, yyyy
diff --git a/day1/exercise/pytriads.py b/day1/exercise/pytriads.py
index 4c47313..bea543d 100644
--- a/day1/exercise/pytriads.py
+++ b/day1/exercise/pytriads.py
@@ -10,12 +10,8 @@ def gcd(a, b):
else:
return gcd(b, a%b)
-a = 3
-while a < 100:
- b = a + 1
- while b < 100:
- is_ps, c = is_perfect_square((a * a) + (b * b))
- if is_ps and gcd(a, b) == 1:
- print a, b, c
- b += 1
- a += 1
+for a in range(3, 501):
+ for b in range( a+1, 501, 2):
+ if gcd( a, b ) == 1:
+ is_ps, c = is_perfect_square((a * a) + (b * b))
+ if is_ps: print a, b, c