We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a855317 commit 5193288Copy full SHA for 5193288
happy-number.go
@@ -0,0 +1,27 @@
1
+package main
2
+
3
+import (
4
+ "fmt"
5
+)
6
7
+func isHappy(n int) bool {
8
+ if n == 1 {
9
+ return true
10
+ }
11
+ m := make(map[int]bool)
12
+ for ; n != 1; m[n] = true {
13
+ sum := 0
14
+ for ; n > 0; n /= 10 {
15
+ sum += (n % 10) * (n % 10)
16
17
+ n = sum
18
+ if _, ok := m[n]; ok {
19
+ return false
20
21
22
23
+}
24
25
+func main() {
26
+ fmt.Println(isHappy(7))
27
0 commit comments