A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
33
Task/Happy-numbers/Go/happy-numbers.go
Normal file
33
Task/Happy-numbers/Go/happy-numbers.go
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func happy(n int) bool {
|
||||
m := make(map[int]int)
|
||||
for n > 1 {
|
||||
m[n] = 0
|
||||
s := strconv.Itoa(n)
|
||||
n = 0
|
||||
for _, d := range s {
|
||||
x := int(d) - '0'
|
||||
n += x * x
|
||||
}
|
||||
if _, ok := m[n]; ok {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func main() {
|
||||
for found, n := 0, 1; found < 8; n++ {
|
||||
if happy(n) {
|
||||
fmt.Print(n, " ")
|
||||
found++
|
||||
}
|
||||
}
|
||||
fmt.Println("")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue