forked from fsouza/euler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
37.go
90 lines (72 loc) · 1.46 KB
/
37.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"fmt"
"strconv"
)
func BuildPrimesList(limit float64) []int {
primes := make([]int, 0)
primesCh := GetPrimes(limit)
for prime := range primesCh {
if prime == 0 {
break
}
primes = append(primes, prime)
}
return primes
}
func IsPresent(slice []int, number int) bool {
for _, element := range slice {
if element == number {
return true
}
}
return false
}
func Truncate(number int, from string) int {
digits := []int(strconv.Itoa(number))
length := len(digits)
if length == 1 {
return 0
}
if from == "right" {
digits = digits[:length - 1]
} else {
digits = digits[1:]
}
newNumber, _ := strconv.Atoi(string(digits))
return newNumber
}
func IsTruncatable(number int, primes []int) bool {
if number < 10 {
return false
}
operations := []string{"right", "left"}
for _, operation := range operations {
copyNumber := number
for copyNumber = Truncate(copyNumber, operation); copyNumber != 0; copyNumber = Truncate(copyNumber, operation) {
if !IsPresent(primes, copyNumber) {
return false
}
}
}
return true
}
func main() {
truncatables := make([]int, 11)
truncatablesCount := 0
primes := BuildPrimesList(1000000)
for _, prime := range primes {
if IsTruncatable(prime, primes) {
truncatables[truncatablesCount] = prime
truncatablesCount++
}
if truncatablesCount > 10 {
break
}
}
sum := 0
for _, truncatable := range truncatables {
sum += truncatable
}
fmt.Println(sum)
}