Another O(sqrt(n)) c++ solution


  • 1
    Z
    class Solution {
    public:
        bool checkPerfectNumber(int num) {
            if (num <= 1) {
                return false;
            }
            int sum = 1, left = 2, right = num / 2;
            while (left < right) {
                if (left * right == num) {
                    sum += left + right;
                }
                ++left;
                right = num / left;
            }
            return sum == num;
        }
    };
    

Log in to reply
 

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