day1/exercise/pytriads.py
author Madhusudan.C.S <madhusudancs@gmail.com>
Sun, 11 Oct 2009 16:51:43 +0530
changeset 94 8c92864c184b
parent 64 333092b68926
child 354 5dc6c3673f9d
permissions -rw-r--r--
Almost last set of official solutions and final quiz.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
64
333092b68926 Added quiz tex file and all exercise problems Madhu worked out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     1
def is_perfect_square(n):
333092b68926 Added quiz tex file and all exercise problems Madhu worked out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     2
    i = 1
333092b68926 Added quiz tex file and all exercise problems Madhu worked out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     3
    while i * i < n:
333092b68926 Added quiz tex file and all exercise problems Madhu worked out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     4
        i += 1
333092b68926 Added quiz tex file and all exercise problems Madhu worked out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     5
    return i * i == n, i
333092b68926 Added quiz tex file and all exercise problems Madhu worked out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     6
333092b68926 Added quiz tex file and all exercise problems Madhu worked out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     7
def gcd(a, b):
333092b68926 Added quiz tex file and all exercise problems Madhu worked out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     8
    if a % b == 0:
333092b68926 Added quiz tex file and all exercise problems Madhu worked out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     9
        return b
333092b68926 Added quiz tex file and all exercise problems Madhu worked out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    10
    else:
333092b68926 Added quiz tex file and all exercise problems Madhu worked out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    11
        return gcd(b, a%b)
333092b68926 Added quiz tex file and all exercise problems Madhu worked out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    12
94
8c92864c184b Almost last set of official solutions and final quiz.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 64
diff changeset
    13
a = 3
8c92864c184b Almost last set of official solutions and final quiz.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 64
diff changeset
    14
while a < 100:
8c92864c184b Almost last set of official solutions and final quiz.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 64
diff changeset
    15
    b = a + 1
8c92864c184b Almost last set of official solutions and final quiz.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 64
diff changeset
    16
    while b < 100:
8c92864c184b Almost last set of official solutions and final quiz.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 64
diff changeset
    17
        is_ps, c = is_perfect_square((a * a) + (b * b))
8c92864c184b Almost last set of official solutions and final quiz.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 64
diff changeset
    18
        if is_ps and gcd(a, b) == 1:
64
333092b68926 Added quiz tex file and all exercise problems Madhu worked out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    19
            print a, b, c
94
8c92864c184b Almost last set of official solutions and final quiz.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 64
diff changeset
    20
        b += 1
8c92864c184b Almost last set of official solutions and final quiz.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 64
diff changeset
    21
    a += 1