Medium
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:
k digits of the number.k digits of the number.Example 1:
Input: num = 23
Output: true
Explanation:
num = 23 are 2 and 23, both are prime.num = 23 are 3 and 23, both are prime.true.Example 2:
Input: num = 39
Output: false
Explanation:
num = 39 are 3 and 39. 3 is prime, but 39 is not prime.num = 39 are 9 and 39. Both 9 and 39 are not prime.false.Example 3:
Input: num = 7
Output: true
Explanation:
true.Constraints:
1 <= num <= 109class Solution {
private fun isPrime(n: Int): Boolean {
if (n != 2 && n % 2 == 0) {
return false
}
var i = 3
while (i * i <= n) {
if (n % i == 0) {
return false
}
i += 2
}
return true
}
fun completePrime(num: Int): Boolean {
var y = 0
var z = 1
var x = num
while (x > 0) {
y = z * (x % 10) + y
if (y == 1 || !isPrime(y)) {
return false
}
if (x == 1 || !isPrime(x)) {
return false
}
x /= 10
z *= 10
}
return true
}
}