My C++ Solution


  • 1
    S
    class Solution {
    public:
        bool checkPerfectNumber(int num) {
            if(num == 1) return false;
            
            int sum = 1;
            int tmpnum = num;
            for(int i = 2; i < tmpnum; i++){
                if(num % i == 0){
                    sum += i;
                    sum += num / i;
                    tmpnum = num / i;
                }
            }
            if( sum == num) return true;
            return false;
        }
    };
    

  • 0

    it is a good idea to limit i of not extending num/i, in order to avoid duplication.


Log in to reply
 

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