Perfect numbers X62099


Statement
 

pdf   zip

A proper divisor of a number is a positive factor of that number other than the number itself. For example, the proper divisors of 6 are 1, 2, and 3. A perfect number is a positive integer that is equal to the sum of its proper positive divisors. For instance, 6=1+2+36 = 1 + 2 + 3 and 28=1+2+4+7+1428 = 1 + 2 + 4 + 7 + 14 are perfect numbers. In contrast, 1,2,31, 2, 3 and 1818 are not perfect.

Write a function @show_perfect(f)@ that given a list ff of integers greater than zero computes the first perfect number that appears in ff, if any. When ff has no perfect numbers the function returns 1-1.

The following function @proper_divisors(n)@ that computes the ordered list of proper divisors of a natural number nn can be helpful.

def proper_divisors(n):
    '''
    n is an integer greater than zero
    returns the ordered list of proper divsisors of n
    >>> proper_divisors(6)
    [1, 2, 3]
    >>> proper_divisors(284)
    [1, 2, 4, 71, 142]
    >>> proper_divisors(1)
    []
    '''
    if n == 1:
        return []
    result = [1]
    d = 2
    while d*d <= n:
        if n%d == 0:
            result.append(d)
            if n//d != d:
                result.append(n//d)
        d += 1
    return sorted(result)

Sample session

Sample session
>>> show_perfect([1, 3, 5, 28, 1, 18, 6])
28
>>> show_perfect([3, 5, 7, 9])
-1
>>> show_perfect([6])
6
>>> show_perfect([2, 10, 15, 1, 6, 5, 28])
6
Information
Author
ProAl
Language
English
Official solutions
Python
User solutions
Python