KatInterview

507. Perfect Number

EasySolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

A perfect number is a positive integer that equals the sum of its proper positive divisors (all positive divisors excluding the number itself). The task is to determine if a given integer num is a perfect number.

Solution

Perfect Number in Python

import math

class Solution:
    def checkPerfectNumber(self, num: int) -> bool:
        # A perfect number must be positive. By definition, 1 is not a perfect number.
        if num <= 1:
            return False
        
        # Start sum with 1, as 1 is a proper divisor for all num > 1.
        sum_of_divisors = 1
        
        # Iterate from 2 up to the integer part of the square root of num.
        for i in range(2, int(math.sqrt(num)) + 1):
            # If i divides num, it's a divisor.
            if num % i == 0:
                sum_of_divisors += i
                # Add the pair divisor num/i, but only if it's not i itself (case of a perfect square).
                if i * i != num:
                    sum_of_divisors += num // i
        
        # A number is perfect if the sum of its proper divisors equals the number itself.
        return sum_of_divisors == num

Perfect Number in Java

class Solution {
    public boolean checkPerfectNumber(int num) {
        // A perfect number must be positive. By definition, 1 is not a perfect number.
        if (num <= 1) {
            return false;
        }

        // Start sum with 1, as 1 is a proper divisor for all num > 1.
        int sum = 1;

        // Iterate from 2 up to the square root of num.
        for (int i = 2; i * i <= num; i++) {
            // If i divides num, it's a divisor.
            if (num % i == 0) {
                sum += i;
                // Add the pair divisor num/i, but only if it's not i itself (case of a perfect square).
                if (i * i != num) {
                    sum += num / i;
                }
            }
        }

        // A number is perfect if the sum of its proper divisors equals the number itself.
        return sum == num;
    }
}

Perfect Number in C++

class Solution {
public:
    bool checkPerfectNumber(int num) {
        // A perfect number must be positive. By definition, 1 is not a perfect number.
        if (num <= 1) {
            return false;
        }
        
        // Start sum with 1, as 1 is a proper divisor for all num > 1.
        int sum = 1;
        
        // Iterate from 2 up to the square root of num.
        for (int i = 2; i * i <= num; ++i) {
            // If i divides num, it's a divisor.
            if (num % i == 0) {
                sum += i;
                // Add the pair divisor num/i, but only if it's not i itself (case of a perfect square).
                if (i * i != num) {
                    sum += num / i;
                }
            }
        }
        
        // A number is perfect if the sum of its proper divisors equals the number itself.
        return sum == num;
    }
};

Perfect Number in TypeScript

function checkPerfectNumber(num: number): boolean {
    // A perfect number must be positive. By definition, 1 is not a perfect number.
    if (num <= 1) {
        return false;
    }

    // Start sum with 1, as 1 is a proper divisor for all num > 1.
    let sum = 1;

    // Iterate from 2 up to the square root of num.
    for (let i = 2; i * i <= num; i++) {
        // If i divides num, it's a divisor.
        if (num % i === 0) {
            sum += i;
            // Add the pair divisor num/i, but only if it's not i itself (case of a perfect square).
            if (i * i !== num) {
                sum += num / i;
            }
        }
    }

    // A number is perfect if the sum of its proper divisors equals the number itself.
    return sum === num;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.