C++ solution


  • 0
    Q
    class Solution {
    public:
        bool checkPerfectNumber(int num) {
            if(num < 3)
                return false;
            int res = 1;
            for(int i=2;i<sqrt(num);i++){
                if(num%i == 0){
                    res += i;
                    res += num/i;
                }
            }
            if(sqrt(num) * sqrt(num) == num)
                res += sqrt(num);
            
            return res == num;
        }
    };
    

Log in to reply
 

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