[507. Perfect Number] C++ AC


  • 0
    class Solution {
    public:
    bool checkPerfectNumber(int num) {
        if(num <= 1) return false;
        int sum = 0;
        int i = 1;
        while(i <= sqrt(num)){
            if(num % i == 0){
                sum += i + num / i;
            }
            i = i + 1;
        }
        return (sum - num) == num;
    }
    };

Log in to reply
 

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