MCPcopy Create free account
hub / github.com/austingebauer/go-leetcode / isHappy

Function isHappy

happy_number_202/solution.go:5–25  ·  view source on GitHub ↗
(n int)

Source from the content-addressed store, hash-verified

3import "math"
4
5func isHappy(n int) bool {
6 cycles := false
7 seen := make(map[int]bool)
8 for n != 1 {
9 _, ok := seen[n]
10 if ok {
11 cycles = true
12 break
13 }
14 seen[n] = true
15
16 sum := 0
17 for n != 0 {
18 sum += int(math.Pow(float64(n%10), 2))
19 n = n / 10
20 }
21 n = sum
22 }
23
24 return !cycles
25}

Callers 1

Test_isHappyFunction · 0.85

Calls

no outgoing calls

Tested by 1

Test_isHappyFunction · 0.68