-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (58 loc) · 1.07 KB
/
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
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"fmt"
"time"
)
func factorial(x int64) int64 {
if x == 0 || x == 1 {
return 1
} else {
return x * factorial(x-1)
}
}
func digitFactorial(x int64) int64 {
digitFactorial := int64(0)
for x > 0 {
digit := x % 10
digitFactorial += factorial(digit)
x /= 10
}
return digitFactorial
}
func LenDigitFactorialChain(memory map[int64]int64, x int64) int64 {
if memory != nil {
l, ok := memory[x]
if ok {
return l
}
}
numsMap := map[int64]struct{}{}
numsMap[x] = struct{}{}
chainLength := int64(1)
next := x
outerLoop:
for {
next = digitFactorial(next)
if _, ok := numsMap[next]; ok {
break outerLoop
}
numsMap[next] = struct{}{}
chainLength++
}
if memory != nil {
memory[x] = chainLength
}
return chainLength
}
func main() {
start := time.Now()
mem := map[int64]int64{}
count := 0
for x := range int64(1e6) {
if LenDigitFactorialChain(mem, x) == 60 {
count++
}
}
elapsed := time.Since(start)
fmt.Printf("Count x for x < 1e6 that (LenDigitFactorialChain(x) = 60) : %d (elapsed = %v)\n", count, elapsed)
}