Simple C++ Solution

  • 0
    class Solution {
        bool checkPerfectNumber(int num) {
            //1 is a special divisor
            if (num==1) return false;
            int sum=1;
            int top=num;
            for (int i=2;i<top;i++){
                if (num%i==0) {
                    top=num/i;  //if i is a divisor, num/i must be a divisor (except i==num/i)
                                //any number bigger than top is considered or not a divisor.
                    if (top>i) sum+=top; 
            if (sum==num) return true;
            return false;

Log in to reply

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