-
Notifications
You must be signed in to change notification settings - Fork 0
/
p119.go
68 lines (57 loc) · 1.04 KB
/
p119.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
67
68
package main
import (
"fmt"
"strconv"
"time"
)
func DigitsSum(x int64) int64 {
sum := int64(0)
for x > 0 {
sum += x % 10
x /= 10
}
return sum
}
func main() {
start := time.Now()
list := []int64{0, 0, 16}
n := 0
// Init the list, at least have 2 digits
for i := int64(3); i < 16; i++ {
list = append(list, i)
for list[i] < 16 {
list[i] *= i
}
if DigitsSum(list[i]) == i {
n++
fmt.Println(n, list[i])
}
}
for n < 30 {
// update the minimum value
list[2] *= 2
if DigitsSum(list[2]) == 2 {
n++
fmt.Println(n, list[2])
}
// add possible bases
for i := int64(len(list)); i < list[2]; i++ {
if int64(len(strconv.FormatInt(list[2], 10))*9) < i {
break
}
list = append(list, i)
}
// update the power of each bases and check its sum of digits
for i := int64(3); i < int64(len(list)); i++ {
if list[i] < list[2] {
list[i] *= i
if DigitsSum(list[i]) == i {
n++
fmt.Println(n, list[i])
}
}
}
}
elapsed := time.Since(start)
fmt.Println("Elasped:", elapsed)
}