Skip to content

Latest commit

 

History

History
28 lines (25 loc) · 762 Bytes

HappyNumbersSolution.md

File metadata and controls

28 lines (25 loc) · 762 Bytes

Happy Numbers Solution

Swift

func sumOfSquares(of n: Int) -> Int {
    var n = n
    var sum = 0
    while n > 0 {
        sum += Int(pow(Double(n % 10), 2.0))
        n /= 10
    }
    return sum
}

func isHappy(n: Int) -> Bool {
    //Unhappy numbers will loop forever.  To catch this, we will have a slow counter and a fast one.                                                                                 
    var slowHappySum = n
    var fastHappySum = n
    repeat {
        slowHappySum = sumOfSquares(of: slowHappySum)
        fastHappySum = sumOfSquares(of: fastHappySum)
        fastHappySum = sumOfSquares(of: fastHappySum)
        if slowHappySum == 1 { return true }
    } while slowHappySum != fastHappySum
    return false
}