Python(96%) & JavaScript


  • 0
    S
    class Solution(object):
        def checkPerfectNumber(self, num):
            """
            :type num: int
            :rtype: bool
            """
            res = 0
            base = 1
            if num <=0: return False
            if num%10==6 or num%10==8:
                if num == 6:
                    return True
                while(res < num):
                    res = res + pow(base,3)
                    base = base + 2
    
            return res == num
    
    var checkPerfectNumber = function(num) {
        var base = 1;
        var res = 0;
        if(num===0) return false;
        if(num%10==6||num%10==8){
          if(num===6) return true;
          while(res<num){
              res += Math.pow(base,3);
              base +=2;
          }
        }
        return res===num;
    };
    

Log in to reply
 

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