| https://leetcode.com/problems/happy-number | Easy |
|---|
class Solution {
private val sq = intArrayOf(0,1,4,9,16,25,36,49,64,81)
private fun next(n: Int): Int {
var x = n
var s = 0
while (x != 0) { s += sq[x % 10]; x /= 10 }
return s
}
fun isHappy(n: Int): Boolean {
var slow = n
var fast = next(n)
while (fast != 1 && slow != fast) {
slow = next(slow)
fast = next(next(fast))
}
return fast == 1
}
}