-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
53 lines (46 loc) · 889 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"fmt"
"time"
)
func squareDigit(x int64) int64 {
result := int64(0)
for x != 0 {
remainder := x % 10
result += remainder * remainder
x /= 10
}
return result
}
var memory map[int64]int64
func endSquareDigitChain(x int64) int64 {
if end, ok := memory[x]; ok {
return end
}
tempSlice := []int64{x}
next := x
for {
next = squareDigit(next)
if next == 1 || next == 89 {
break
}
tempSlice = append(tempSlice, next)
}
for _, e := range tempSlice {
memory[e] = next
}
return next
}
func main() {
memory = make(map[int64]int64)
start := time.Now()
endsIn89 := 0
for i := int64(1); i <= 1e7; i++ {
end := endSquareDigitChain(i)
if end == 89 {
endsIn89++
}
}
elapsed := time.Since(start)
fmt.Printf("numbers below ten million that their square digit chain will arrive at 89 = %v (elapsed = %v)\n", endsIn89, elapsed)
}