Easy understood solution


  • 0
    class Solution {
    public:
    	bool checkPerfectNumber(int num) {
    		set<int> se;
    		int i, ans=0;
    		if (num < 6) return false;
    		se.insert(1);
    		for(i=2; i<sqrt(num)+1; i++)
    		{
    			if (num % i == 0)
    			{
    				se.insert(i);
    				se.insert(num / i);
    			} 
    
    		}
    		for (auto i: se)
    		{
    			ans += i;
    		}
    		return ans==num;
    	}	
    };
    

Log in to reply
 

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