Simple accepted swift solution


  • 0
    S
    func checkPerfectNumber(_ num: Int) -> Bool {
            guard num > 4 else {
                return false
            }
            let root = Int(sqrt(Double(num)))
            var sum = 1
            for i in 2...root {
                if num % i == 0 {
                    sum += i
                    if i != num/i {
                        sum += num/i
                    }
                }            
            }
            return sum == num
        }
    

Log in to reply
 

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