-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path0866-prime-palindrome.js
55 lines (52 loc) · 1.4 KB
/
0866-prime-palindrome.js
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
/**
* 866. Prime Palindrome
* https://leetcode.com/problems/prime-palindrome/
* Difficulty: Medium
*
* Given an integer n, return the smallest prime palindrome greater than or equal to n.
*
* An integer is prime if it has exactly two divisors: 1 and itself. Note that 1 is not a prime
* number.
* - For example, 2, 3, 5, 7, 11, and 13 are all primes.
*
* An integer is a palindrome if it reads the same from left to right as it does from right to left.
* - For example, 101 and 12321 are palindromes.
*
* The test cases are generated so that the answer always exists and is in the range [2, 2 * 108].
*/
/**
* @param {number} n
* @return {number}
*/
var primePalindrome = function(start) {
while (true) {
const numStr = start.toString();
if (numStr.length % 2 === 0 && start > 11) {
start = 10 ** Math.ceil(Math.log10(start + 1));
continue;
}
if (!isPalindrome(numStr)) {
start++;
continue;
}
if (isPrime(start)) return start;
start++;
}
};
function isPrime(num) {
if (num <= 1) return false;
if (num <= 3) return true;
if (num % 2 === 0 || num % 3 === 0) return false;
for (let i = 3; i <= Math.sqrt(num) + 1; i += 2) {
if (num % i === 0) return false;
}
return true;
}
function isPalindrome(str) {
let left = 0;
let right = str.length - 1;
while (left < right) {
if (str[left++] !== str[right--]) return false;
}
return true;
}