Simple python solution


  • 0
    X
    #!/usr/bin/env python                                                            
    #-*- coding:utf-8 -*-                                                            
    from math import ceil                                                            
    class Solution(object):                                                          
        def checkPerfectNumber(self, num):                                              
            """                                                                                          
            :type num: int                                                              
            :rtype: bool                                                                
            """                                                                         
            if num <= 1:                                                                
                return False                                                            
            sqrt = num ** 0.5                                                           
            divisors = [1]                                                              
            for i in xrange(2,int(ceil(sqrt))):                                         
                if num % i == 0:                                                        
                    divisor = num==i*i and [i] or [i,num/i]                             
                    divisors.extend(divisor)                                            
            if sum(divisors) == num:                                                    
                return True                                                             
            else:                                                                       
                return False                                                            
                                                                                        
    if __name__ == "__main__":                                                          
        s = Solution()                                                                  
        for i in xrange(100):                                                           
            print i,s.checkPerfectNumber(i) 
    

Log in to reply
 

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