-
Notifications
You must be signed in to change notification settings - Fork 0
/
p38.go
72 lines (61 loc) · 1.1 KB
/
p38.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
69
70
71
72
package main
import (
"fmt"
"sort"
"strconv"
"time"
)
func Dtoi(digits []int, len int) int {
sum := 0
for i := 0; i < len; i++ {
sum = sum*10 + digits[i]
}
return sum
}
func main() {
start := time.Now()
digits := make([]int, 9)
// init
for i := 0; i < 9; i++ {
digits[i] = 9 - i
}
done := false
for !done {
for i := 1; !done && i < 5; i++ {
base := Dtoi(digits[:i], i)
for j, n := i, 2; j < 9; n++ {
str := strconv.Itoa(base * n)
if j+len(str) < 10 {
if Dtoi(digits[j:j+len(str)], len(str)) == base*n {
done = true
} else {
done = false
break
}
j += len(str)
} else {
done = false
break
}
}
if done {
fmt.Println(Dtoi(digits, 9), base)
}
}
for i := 9 - 1; i > 0; i-- {
if digits[i] < digits[i-1] {
for j := 9 - 1; j > i-1; j-- {
if digits[j] < digits[i-1] {
digits[j], digits[i-1] = digits[i-1], digits[j]
break
}
}
// reorder
sort.Sort(sort.Reverse(sort.IntSlice(digits[i:9])))
break
}
}
}
elapsed := time.Since(start)
fmt.Println("Elasped:", elapsed)
}