-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3765-CompletePrimeNumber.go
More file actions
148 lines (134 loc) · 4.84 KB
/
3765-CompletePrimeNumber.go
File metadata and controls
148 lines (134 loc) · 4.84 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
// 3765. Complete Prime Number
// You are given an integer num.
// A number num is called a Complete Prime Number if every prefix and every suffix of num is prime.
// Return true if num is a Complete Prime Number, otherwise return false.
// Note:
// 1. A prefix of a number is formed by the first k digits of the number.
// 2. A suffix of a number is formed by the last k digits of the number.
// 3. A prime number is a natural number greater than 1 with only two factors, 1 and itself.
// 4. Single-digit numbers are considered Complete Prime Numbers only if they are prime.
// Example 1:
// Input: num = 23
// Output: true
// Explanation:
// Prefixes of num = 23 are 2 and 23, both are prime.
// Suffixes of num = 23 are 3 and 23, both are prime.
// All prefixes and suffixes are prime, so 23 is a Complete Prime Number and the answer is true.
// Example 2:
// Input: num = 39
// Output: false
// Explanation:
// Prefixes of num = 39 are 3 and 39. 3 is prime, but 39 is not prime.
// Suffixes of num = 39 are 9 and 39. Both 9 and 39 are not prime.
// At least one prefix or suffix is not prime, so 39 is not a Complete Prime Number and the answer is false.
// Example 3:
// Input: num = 7
// Output: true
// Explanation:
// 7 is prime, so all its prefixes and suffixes are prime and the answer is true.
// Constraints:
// 1 <= num <= 10^9
import "fmt"
import "strconv"
import "math"
func completePrime(num int) bool {
divisor, suffix, prefix := 1, num, num
for num >= 10 {
num /= 10
divisor *= 10
}
// Time complexity: O(sqrt(num))
isPrime := func(num int) bool {
if num <= 1 { return false }
if num == 2 { return true } // Check 2 separately
if num % 2 == 0 { return false }
for i := 3; i * i <= num; i += 2 { // Check for odd divisors up to sqrt(num)
if num % i == 0 {
return false
}
}
return true
}
for prefix > 0 {
if !isPrime(prefix) {
return false
}
prefix %= divisor
divisor /= 10
}
for suffix > 0 {
if !isPrime(suffix) {
return false
}
suffix /= 10
}
return true
}
func completePrime1(num int) bool {
str := strconv.Itoa(num)
n := len(str)
isPrime := func(n int) bool {
if n < 2 { return false } // 0和1不是质数
if n == 2 || n == 3 { return true } // 2和3是质数
if n % 2 == 0 || n % 3 == 0 { return false } // 排除偶数(除2外)和能被3整除的数
if n % 6 != 1 && n % 6 != 5 { return false } // 所有大于5的质数必位于6x±1两侧(如5,7,11,13)
for i := (5); i <= int(math.Sqrt(float64(n))) ; i += 6 { // 只需检查到 √n 范围内的因子, 步长为6,检查i和i+2
if n % i == 0 || n % (i + 2) == 0 {
return false
}
}
return true
}
for i := 0; i < n; i++ { // prefix
prefix, _ := strconv.Atoi(str[i:])
if !isPrime(prefix) {
return false
}
}
for i := n; i > 0; i-- { // subfix
subfix, _ := strconv.Atoi(str[:i])
// fmt.Printf("subfix[:%d]=%d\n", i, subfix)
if !isPrime(subfix) {
return false
}
}
return true
}
func main() {
// Example 1:
// Input: num = 23
// Output: true
// Explanation:
// Prefixes of num = 23 are 2 and 23, both are prime.
// Suffixes of num = 23 are 3 and 23, both are prime.
// All prefixes and suffixes are prime, so 23 is a Complete Prime Number and the answer is true.
fmt.Println(completePrime(23)) // true
// Example 2:
// Input: num = 39
// Output: false
// Explanation:
// Prefixes of num = 39 are 3 and 39. 3 is prime, but 39 is not prime.
// Suffixes of num = 39 are 9 and 39. Both 9 and 39 are not prime.
// At least one prefix or suffix is not prime, so 39 is not a Complete Prime Number and the answer is false.
fmt.Println(completePrime(39)) // false
// Example 3:
// Input: num = 7
// Output: true
// Explanation:
// 7 is prime, so all its prefixes and suffixes are prime and the answer is true.
fmt.Println(completePrime(7)) // true
fmt.Println(completePrime(1)) // false
fmt.Println(completePrime(13)) // false
fmt.Println(completePrime(1024)) // false
fmt.Println(completePrime(1_000_000_000)) // false
fmt.Println(completePrime(1_000_000_007)) // false
fmt.Println(completePrime1(23)) // true
fmt.Println(completePrime1(39)) // false
fmt.Println(completePrime1(7)) // true
fmt.Println(completePrime1(1)) // false
fmt.Println(completePrime1(13)) // false
fmt.Println(completePrime1(1024)) // false
fmt.Println(completePrime1(1_000_000_000)) // false
fmt.Println(completePrime1(1_000_000_007)) // false
}