LeetCode #507 Perfect Number Solution & Explanation

LeetCode Problem

perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. A divisor of an integer x is an integer that can divide x evenly.

Given an integer n, return true if n is a perfect number, otherwise return false.

LeetCode #507 Examples
LeetCode #507 Constraints

Solution

Let’s use a easy iteration try to get the answer.

Iterate from 1 to num, to check all it’s positive divisors and sum them together.

C# Solution

First try

public class Solution {
    public bool CheckPerfectNumber(int num) {
        if(num == 1)
        {
            return false;
        }
        
        int sum = 0;
        
        for(int i =1; i<num;i++)
        {
            if(num%i ==0)
            {
                sum+=i;
            }
        }
        
        return num == sum;
    }
}

But, what we got? Time Limit Exceeded Error !!!!

Solution1 – iterate to square root of num

If num has divisor, than it can be write as a * b

And when will we get the smallest difference between a and b ?

That will be num1/2

Why we need to know this, that is because if a divisor is smaller than num1/2 , we will find a divisor greater than num1/2 ➡ we can check the iterate to num1/2 instead of num

public class Solution {
    public bool CheckPerfectNumber(int num) {
        if(num == 1)      
        {
            return false;
        }
        
        int sum = 1;
        int max = (int)Math.Sqrt(num);  
        //int max = (int)Math.Pow(num,0.5);  //you can choose which you want
        
        for(int i =2; i<=max;i++)
        {
            if(num%i ==0)
            {
                sum+= i + num/i;
            }
        }
        return num == sum;
    }
}

Java Solution

Solution1 – iterate to square root of num

class Solution {
    public boolean checkPerfectNumber(int num) {
        if(num == 1)      
        {
            return false;
        }
        
        int sum = 1;
        int max = (int)Math.sqrt(num);
        //int max = (int)Math.pow(num,0.5);
        
        for(int i =2; i<=max;i++)
        {
            if(num%i ==0)
            {
                sum+= i + num/i;       
            }
        }
        return num == sum;
    }
}

Python3 Solution

Solution1 – iterate to square root of num

class Solution:
    def checkPerfectNumber(self, num: int) -> bool:
        if num == 1:
            return False
        
        sum = 1;
        max = math.ceil(math.sqrt(num))
        #max = math.ceil(num**0.5)
        
        for i in range(2,max):
            if num%i ==0:
                sum+= i + num/i
                
        return num == sum

JavaScript Solution

Solution1 – iterate to square root of num

/**
 * @param {number} num
 * @return {boolean}
 */
var checkPerfectNumber = function(num) {
        if(num == 1)      
        {
            return false;
        }
        
        var sum = 1;
        var max = parseInt(Math.sqrt(num));
        //var max = parseInt(Math.pow(num,0.5));
        
        for(let i =2; i<=max;i++)
        {
            if(num%i ==0)
            {
                sum+= i + num/i;       
            }
        }
        return num == sum;
};

Conclusion

🧡If my solution helps, that is my honor!

🧡You can support me by clicking some ad, Thanks a lot

If you got any problem about the explanation or you need other programming language solution, please feel free to let me know (either leave a comment or contact me by Messenger is ok!)

“If everything was perfect, you would never learn and you would never grow.”

Beyonce Knowles

The problem link : Perfect Number – LeetCode

Latest Post

Leave a Reply

Your email address will not be published. Required fields are marked *