Leetcode Note: Go - Happy Number

Happy Number - LeetCode
https://leetcode.com/problems/happy-number/

  • Go 言語で取り組んだメモ

所感

  • Happy Number ってなに?

回答

simple go solution - LeetCode Discuss
https://leetcode.com/problems/happy-number/discuss/450282/simple-go-solution

func isHappy(n int) bool {    
    record := map[int]bool{n: true}
    
    for n != 1 {
        var sum int
        for n != 0 {
            num := n%10
            sum += num * num
            n /= 10
        }
        n = sum
        if record[n] {
            return false
        }
        record[n] = true
    }
    
    return true
}

ハッピー数 - Wikipedia
https://ja.wikipedia.org/wiki/%E3%83%8F%E3%83%83%E3%83%94%E3%83%BC%E6%95%B0

ハッピー数という概念を初めて知った