-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (50 loc) · 1014 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
54
55
56
57
58
59
60
package main
import (
"fmt"
"strings"
"time"
)
func isPandigital(a, b int) bool {
digit := make(map[rune]struct{})
aStr := fmt.Sprintf("%d", a)
bStr := fmt.Sprintf("%d", b)
cStr := fmt.Sprintf("%d", a*b)
l := len(aStr) + len(bStr) + len(cStr)
if l != 9 {
return false
}
aStr = strings.ReplaceAll(aStr, "0", "")
bStr = strings.ReplaceAll(bStr, "0", "")
cStr = strings.ReplaceAll(cStr, "0", "")
for _, i := range aStr {
digit[i] = struct{}{}
}
for _, i := range bStr {
digit[i] = struct{}{}
}
for _, i := range cStr {
digit[i] = struct{}{}
}
if len(digit) == 9 {
return true
}
return false
}
func main() {
start := time.Now()
products := make(map[int]struct{})
for i := range 10000 {
for j := range 100 {
_, ok := products[i*j]
if isPandigital(i, j) && !ok {
products[i*j] = struct{}{}
}
}
}
sum := 0
for key := range products {
sum += key
}
elapsed := time.Since(start)
fmt.Printf("sum of pandigital products=%d (elapsed=%v)\n", sum, elapsed)
}