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 <= 109public class Solution {
private boolean isPrime(int n) {
if (n != 2 && n % 2 == 0) {
return false;
}
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0) {
return false;
}
}
return true;
}
public boolean completePrime(int num) {
int y = 0;
int z = 1;
int 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;
}
}