```
var middle:[Int:Bool] = [:]
func isHappy(n: Int) -> Bool {
var n = n
if n == 1 {
return true
}
if middle[n] != nil {
return false
}
middle[n] = true
var nextN = 0
while n != 0 {
nextN += (n % 10) * (n % 10)
n = n / 10
}
return isHappy(nextN)
}
```