day1/exercise/aliquot.py
author Santosh G. Vattam <vattam.santosh@gmail.com>
Thu, 11 Mar 2010 18:01:23 +0530
changeset 380 669b72283b55
parent 354 5dc6c3673f9d
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 aliquot(n):
380
669b72283b55 Updated after Day 2 at GRDCS
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 354
diff changeset
     2
    """returns the aliquot of a number which
669b72283b55 Updated after Day 2 at GRDCS
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 354
diff changeset
     3
    is defined as the sum of all the proper
669b72283b55 Updated after Day 2 at GRDCS
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 354
diff changeset
     4
    divisors of a number"""
94
8c92864c184b Almost last set of official solutions and final quiz.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 64
diff changeset
     5
    sum = 1
8c92864c184b Almost last set of official solutions and final quiz.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 64
diff changeset
     6
    i = 2
8c92864c184b Almost last set of official solutions and final quiz.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 64
diff changeset
     7
354
5dc6c3673f9d Changes made during REC Chennai workshop.
Puneeth Chaganti <punchagan@fossee.in>
parents: 94
diff changeset
     8
    while i * i < n: 
64
333092b68926 Added quiz tex file and all exercise problems Madhu worked out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     9
        if n % i == 0:
94
8c92864c184b Almost last set of official solutions and final quiz.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 64
diff changeset
    10
            sum += i + (n / i)
8c92864c184b Almost last set of official solutions and final quiz.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 64
diff changeset
    11
        i += 1
380
669b72283b55 Updated after Day 2 at GRDCS
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 354
diff changeset
    12
    if i*i == n: sum += i
64
333092b68926 Added quiz tex file and all exercise problems Madhu worked out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    13
    return sum
333092b68926 Added quiz tex file and all exercise problems Madhu worked out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    14
94
8c92864c184b Almost last set of official solutions and final quiz.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 64
diff changeset
    15
n = int(raw_input('Enter a number? '))
8c92864c184b Almost last set of official solutions and final quiz.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 64
diff changeset
    16
print aliquot(n)