day1/exercise/pytriads.py
author Santosh G. Vattam <vattam.santosh@gmail.com>
Thu, 11 Mar 2010 18:01:23 +0530
changeset 380 669b72283b55
parent 354 5dc6c3673f9d
child 428 ee689f7c3122
permissions -rw-r--r--
Updated after Day 2 at GRDCS
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
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):
669b72283b55 Updated after Day 2 at GRDCS
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 354
diff changeset
    14
    for b in range( a+1, 101, 2):
354
5dc6c3673f9d Changes made during REC Chennai workshop.
Puneeth Chaganti <punchagan@fossee.in>
parents: 94
diff changeset
    15
        if gcd( a, b ) == 1:
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))
5dc6c3673f9d Changes made during REC Chennai workshop.
Puneeth Chaganti <punchagan@fossee.in>
parents: 94
diff changeset
    17
            if is_ps: print a, b, c