-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0037.go
46 lines (37 loc) · 1.06 KB
/
0037.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
package main
import (
"fmt"
"strconv"
"time"
"github.com/inkxk/project-euler/util"
)
// problem 37
func TruncatablePrimes() {
defer util.TimeProcess(time.Now())
sum := 0
primes := []int{}
out:
for i := 10; len(primes) < 11; i++ {
if util.IsPrimeNumber(i) {
iStr := fmt.Sprintf("%v", i)
isTruncatablePrimes := true
for j := 1; j < len(iStr); j++ {
// ตัด string left and right ทีละ 1 ตัว
subLeft, _ := strconv.Atoi(iStr[:len(iStr)-j])
subRight, _ := strconv.Atoi(iStr[j:])
// ถ้าไม่เป็น prime, ข้าม i ตัวนี้ไปทำตัวอื่นต่อ
if !util.IsPrimeNumber(subLeft) || !util.IsPrimeNumber(subRight) {
isTruncatablePrimes = false
continue out
}
}
// ถ้า loop ผ่านบรรทัดที่ 30 มาได้ทุกตัว จะมาเข้าบรรทัดนี้
if isTruncatablePrimes {
primes = append(primes, i)
sum += i
}
}
}
fmt.Println("primes", primes)
fmt.Println("answer:", sum)
}