Skip to content

Commit 0ad4c9d

Browse files
committed
Time: 0 ms (100%), Space: 17.7 MB (100%) - LeetHub
1 parent 0e0b0b6 commit 0ad4c9d

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# time complexity: O(n * 10 ^ (n/2))
2+
# space complexity: O(n)
3+
class Solution:
4+
def completePrime(self, num: int) -> bool:
5+
def isPrime(n):
6+
if n <= 1:
7+
return False
8+
if n <= 3:
9+
return True
10+
if n % 2 == 0 or n % 3 == 0:
11+
return False
12+
i = 5
13+
while i * i <= n:
14+
if n % i == 0 or n % (i + 2) == 0:
15+
return False
16+
i += 6
17+
return True
18+
s = str(num)
19+
n = len(s)
20+
for k in range(1, n + 1):
21+
prefixVal = int(s[:k])
22+
if not isPrime(prefixVal):
23+
return False
24+
for k in range(n):
25+
suffixVal = int(s[k:])
26+
if not isPrime(suffixVal):
27+
return False
28+
return True
29+
30+
31+
num = 23
32+
print(Solution().completePrime(num))
33+
num = 39
34+
print(Solution().completePrime(num))
35+
num = 7
36+
print(Solution().completePrime(num))

0 commit comments

Comments
 (0)