day1/exercise/pytriads.py
author Christopher Burns <chris.d.burns@gmail.com>
Mon, 28 Jun 2010 23:17:31 -0500
branchscipy2010
changeset 428 ee689f7c3122
parent 380 669b72283b55
permissions -rw-r--r--
REF: Minor formatting tweaks in exercise solutions.
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:
428
ee689f7c3122 REF: Minor formatting tweaks in exercise solutions.
Christopher Burns <chris.d.burns@gmail.com>
parents: 380
diff changeset
    11
        return gcd(b, a % b)
64
333092b68926 Added quiz tex file and all exercise problems Madhu worked out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    12
380
669b72283b55 Updated after Day 2 at GRDCS
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 354
diff changeset
    13
for a in range(3, 101):
428
ee689f7c3122 REF: Minor formatting tweaks in exercise solutions.
Christopher Burns <chris.d.burns@gmail.com>
parents: 380
diff changeset
    14
    for b in range(a + 1, 101, 2):
ee689f7c3122 REF: Minor formatting tweaks in exercise solutions.
Christopher Burns <chris.d.burns@gmail.com>
parents: 380
diff changeset
    15
        if gcd(a, b) == 1:
354
5dc6c3673f9d Changes made during REC Chennai workshop.
Puneeth Chaganti <punchagan@fossee.in>
parents: 94
diff changeset
    16
            is_ps, c = is_perfect_square((a * a) + (b * b))
428
ee689f7c3122 REF: Minor formatting tweaks in exercise solutions.
Christopher Burns <chris.d.burns@gmail.com>
parents: 380
diff changeset
    17
            if is_ps:
ee689f7c3122 REF: Minor formatting tweaks in exercise solutions.
Christopher Burns <chris.d.burns@gmail.com>
parents: 380
diff changeset
    18
                print a, b, c