Almost last set of official solutions and final quiz.
authorMadhusudan.C.S <madhusudancs@gmail.com>
Sun, 11 Oct 2009 16:51:43 +0530
changeset 94 8c92864c184b
parent 93 27b67b50280b
child 98 1b9a427f41c6
child 99 6db0c0608aa2
Almost last set of official solutions and final quiz.
day1/exercise/aliquot.py
day1/exercise/amicable.py
day1/exercise/even_perfect_4a.py
day1/exercise/pytriads.py
quiz.tex
--- a/day1/exercise/aliquot.py	Fri Oct 09 22:57:00 2009 +0530
+++ b/day1/exercise/aliquot.py	Sun Oct 11 16:51:43 2009 +0530
@@ -1,8 +1,19 @@
+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, (n/2)+1):
+    sum = 1
+    i = 2
+
+    is_ps, root = is_perfect_square(n)
+    while i < root:
         if n % i == 0:
-            sum += i
+            sum += i + (n / i)
+        i += 1
     return sum
 
-print aliquot(14)
+n = int(raw_input('Enter a number? '))
+print aliquot(n)
--- 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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day1/exercise/even_perfect_4a.py	Sun Oct 11 16:51:43 2009 +0530
@@ -0,0 +1,14 @@
+def all_digits_even(n):
+    if n < 0: n = -n
+    while n > 0:
+        if n % 2 == 1:
+            return False
+        n /= 10
+    return True
+
+i = 46
+while i <= 94:
+    square = i * i
+    if all_digits_even(square):
+        print square
+    i += 1
--- a/day1/exercise/pytriads.py	Fri Oct 09 22:57:00 2009 +0530
+++ b/day1/exercise/pytriads.py	Sun Oct 11 16:51:43 2009 +0530
@@ -10,9 +10,12 @@
     else:
         return gcd(b, a%b)
 
-for a in range(3, 100):
-    for b in range(a+1, 100):
-        ips, c = is_perfect_square((a * a) + (b * b))
-        if ips and gcd(a, b) == 1:
+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
--- a/quiz.tex	Fri Oct 09 22:57:00 2009 +0530
+++ b/quiz.tex	Sun Oct 11 16:51:43 2009 +0530
@@ -43,6 +43,16 @@
   \titlepage
 \end{frame}
 
+\begin{frame}
+  \frametitle{Write your details...}
+On the top right hand corner please write down the following:
+  \begin{itemize}
+    \item  Name:
+    \item Affliation:
+    \item Occupation:
+  \end{itemize}
+\end{frame}
+
 \begin{frame}{}
   What is the largest integer value that can be represented by Python?
 \end{frame}
@@ -71,7 +81,7 @@
 a = False
 b = True
 c = True
-if a and b or c:
+if (a and b) or c:
     print "You are correct!"
   \end{lstlisting}
 \end{frame}
@@ -101,7 +111,7 @@
 \end{frame}
 
 \begin{frame}{}
-  How do you get the alternate elements of a list?
+  How do you get the alternate elements of a list \emph{p}?
 \end{frame}
 
 \begin{frame}{}
@@ -115,7 +125,7 @@
 \begin{frame}[fragile]{}
   \begin{lstlisting}
   >>> a = (1, 2, 5, 7)
-  >>> a[1]
+  >>> a[1] = 3
   \end{lstlisting}
   What is the output?
 \end{frame}
@@ -137,7 +147,6 @@
     2 c
     3 d
   \end{lstlisting}
-  by doing a small modification in the same two lines of code above?
 \end{frame}
 
 \begin{frame}{}