JavaScript O(√n) solution


  • 0
    var checkPerfectNumber = function(num) {
        let sum = 1;
        for (let i = 2, stop = Math.sqrt(num); i <= stop; i++) {
            if (num % i === 0) sum += i + num / i;
        }
        return num > 1 && sum === num;
    };
    

    Returning false for odd num is a reasonable optimization, but it's unproven that there are no odd perfect numbers.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.