-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem37.go
83 lines (74 loc) · 1.77 KB
/
problem37.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
package projecteuler
import "math"
// truncates specified number of digits from left
// side of number
func trucateFromLeft(num int, pos int) int {
return numFromDigits(splitDigits(num)[pos:], 10)
}
// checks whether all numbers generated by truncating
// left most digit ( one at a time ), are prime or not
func isLeftTruncatablePrime(num int) bool {
check := true
for num > 0 {
if !isPrime(num) {
check = false
break
}
num = trucateFromLeft(num, 1)
}
return check
}
// truncates specified number of digits
// from right side of number
func trucateFromRight(num int, pos int) int {
return numFromDigits(splitDigits(num)[:int(math.Floor(math.Log10(float64(num))))+1-pos], 10)
}
// checks whether all numbers generated by truncating
// right most digit ( one at a time ), are prime or not
func isRightTruncatablePrime(num int) bool {
check := true
for num > 0 {
if !isPrime(num) {
check = false
break
}
num = trucateFromRight(num, 1)
}
return check
}
// checks whether number is both left and right truncatable or not
func isTruncatablePrime(num int) bool {
if num < 10 {
return false
}
return isLeftTruncatablePrime(num) && isRightTruncatablePrime(num)
}
// checks whether any digit of given number is `0` or not
func hasDigitZero(num int) bool {
check := false
for _, v := range splitDigits(num) {
if v == 0 {
check = true
break
}
}
return check
}
// considering only those numbers which aren't having
// `0` as one of their digit
func isValidNumber(num int) bool {
return !hasDigitZero(num)
}
// TrunatablePrimes - Calculates sum of first 11 fully truncatable primes
func TrunatablePrimes() int {
sum := 0
curN := 10
for c := 0; c < 11; {
if isValidNumber(curN) && isTruncatablePrime(curN) {
sum += curN
c++
}
curN++
}
return sum
}