C++ Math Solution


  • 0
    Y
    class Solution {
    public:
        bool checkPerfectNumber(int num) {
            if(num <= 1)
                return false;
            int sum = 0, n = sqrt(num);
            for(int i = 2; i <= n; ++ i){
                if(num%i == 0)
                    sum += (i + (i*i == num?0:num/i));
            }
            ++ sum;
            return sum == num;
        }
    };
    

Log in to reply
 

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