Check Perfect Number using Python

The problem of checking perfect numbers is a popular coding interview question. A perfect number is a positive integer equal to the sum of its positive divisors, excluding the number itself. In this problem, you will be given an integer, and you need to return whether the integer is a perfect number or not. So, if you want to know how to solve this problem, this article is for you. In this article, I will take you through how to check whether a number is a Perfect Number using Python.

Perfect Number Problem

In coding interviews, you will be given a number, and you need to check whether the number is equal to the sum of its divisors, excluding the number itself. For example, 6 is a perfect number because its divisors (excluding itself) are 1, 2, and 3, and 1 + 2 + 3 = 6. If it’s a perfect number, your output should return True, otherwise False. For example, look at the input and output values of this problem shown below:

  • Input: 28 | Output: True
  • Input: 7 | Output: False

Check Perfect Number using Python

I hope you have understood what the problem of checking the perfect number means. Below is how you can solve this problem using the Python programming language:

def checkPerfectNumber(num):
    if num <= 1:
        return False
    div_sum = 1
    i = 2
        
    # Check divisors up to square root of num
    while i*i <= num:
        if num % i == 0:
            div_sum += i
            if i != num//i:
                div_sum += num//i
        i += 1
    # Check if num is a perfect number
    if div_sum == num:
        return True
    else:
        return False
print(checkPerfectNumber(28))
Output: True

Below is how the above code works:

  1. The function starts by checking if num is less than or equal to 1 because if it is, it can’t be a perfect number because a “perfect number” must be a positive integer. So the function returns False.
  2. The function then adds up all the divisors of num, except for num itself. It starts with the smallest possible divisor (2) and checks up to the square root of num. If num has a divisor greater than its square root, it must also have a corresponding divisor less than its square root. For example, if n is 16, its square root is 4, and its divisors are 1, 2, 4, 8, and 16. The function only needs to check up to 4 because any divisor greater than 4 (such as 8) corresponds to a divisor less than 4 (such as 2).
  3. The function then checks whether each number from 2 to the square root of num is a divisor of num by checking whether num is divisible by that number with no remainder (using the % operator). If the number is a divisor of num, the function adds it to a running total called div_sum.
  4. If the number is not equal to num divided by that number, then the function adds num divided by that number to div_sum.
  5. After checking all the divisors of num, the function checks whether the sum of divisors (div_sum) is equal to num. If it is, then num is a perfect number, and the function returns True. Otherwise, the function returns False.

So this is how you can check perfect numbers using Python. You can find many more practice questions for coding interviews solved and explained using Python here.

Summary

A perfect number is a positive integer equal to the sum of its positive divisors, excluding the number itself. In this problem, you will be given an integer, and you need to return whether the integer is a perfect number or not. I hope you liked this article on how to check whether a number is a Perfect Number or not. Feel free to ask valuable questions in the comments section below.

Aman Kharwal
Aman Kharwal

I'm a writer and data scientist on a mission to educate others about the incredible power of data📈.

Articles: 1534

One comment

  1. def perfnum(x):
    t=0
    for i in range(1,x):
    if(x%i==0):
    t=t+i
    else:
    next
    if(t==x):
    return “TRUE”
    else:
    return “FALSE”
    print(perfnum(28))

    OUTPUT:TRUE

Leave a Reply