luoheng
10/12/2019 - 7:40 AM

isHappy


func square(n int) int {
    sum := 0
    for n != 0 {
        d := n % 10
        n = n / 10
        sum += d * d
    }
    return sum
}

func isHappy(n int) bool {
    c := map[int]bool{}
    for !c[n] && n != 1 {
        c[n] = true
        n = square(n)
    }
    return n == 1
}