-
Notifications
You must be signed in to change notification settings - Fork 0
/
euler032.go
59 lines (53 loc) · 1.16 KB
/
euler032.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
package main
import (
"fmt"
"github.com/blevz/euler/common"
)
//Returns true if none of the digits have been before
//Returns false if a zero is seen or a digit has been seen before
func checkDigits(num int, digits *[]bool) bool {
for num != 0 {
if num%10 == 0 {
return false
}
v := (*digits)[num%10-1]
if v {
return false
}
(*digits)[num%10-1] = true
num /= 10
}
return true
}
func AllTrue(digits []bool) bool {
for _, v := range digits {
if !v {
return false
}
}
return true
}
func main() {
products := make(map[int]struct{})
for a := 1; a < common.Pow(10, 8); a++ {
aDigits := common.NumDigits(a)
for b := a; b < common.Pow(10, 9-aDigits); b++ {
bDigits := common.NumDigits(b)
mul := a * b
//If there are two many digits now, stop the inner loop
//B cannot get any bigger and satisfy the constraints
if bDigits+aDigits+common.NumDigits(mul) > 9 {
break
}
digits := make([]bool, 9, 9)
if (checkDigits(a, &digits) && checkDigits(b, &digits) && checkDigits(mul, &digits)) && AllTrue(digits) {
products[mul] = struct{}{}
}
}
}
sum := 0
for k, _ := range products {
sum += k
}
fmt.Println(sum)
}