|
| 1 | +# [866. Prime Palindrome (Medium)](https://leetcode.com/problems/prime-palindrome/) |
| 2 | + |
| 3 | +<p>Find the smallest prime palindrome greater than or equal to <code>N</code>.</p> |
| 4 | + |
| 5 | +<p>Recall that a number is <em>prime</em> if it's only divisors are 1 and itself, and it is greater than 1. </p> |
| 6 | + |
| 7 | +<p>For example, 2,3,5,7,11 and 13 are primes.</p> |
| 8 | + |
| 9 | +<p>Recall that a number is a <em>palindrome</em> if it reads the same from left to right as it does from right to left. </p> |
| 10 | + |
| 11 | +<p>For example, 12321 is a palindrome.</p> |
| 12 | + |
| 13 | +<p> </p> |
| 14 | + |
| 15 | +<div> |
| 16 | +<p><strong>Example 1:</strong></p> |
| 17 | + |
| 18 | +<pre><strong>Input: </strong><span id="example-input-1-1">6</span> |
| 19 | +<strong>Output: </strong><span id="example-output-1">7</span> |
| 20 | +</pre> |
| 21 | + |
| 22 | +<div> |
| 23 | +<p><strong>Example 2:</strong></p> |
| 24 | + |
| 25 | +<pre><strong>Input: </strong><span id="example-input-2-1">8</span> |
| 26 | +<strong>Output: </strong><span id="example-output-2">11</span> |
| 27 | +</pre> |
| 28 | + |
| 29 | +<div> |
| 30 | +<p><strong>Example 3:</strong></p> |
| 31 | + |
| 32 | +<pre><strong>Input: </strong><span id="example-input-3-1">13</span> |
| 33 | +<strong>Output: </strong><span id="example-output-3">101</span></pre> |
| 34 | +</div> |
| 35 | +</div> |
| 36 | +</div> |
| 37 | + |
| 38 | +<p> </p> |
| 39 | + |
| 40 | +<p><strong>Note:</strong></p> |
| 41 | + |
| 42 | +<ul> |
| 43 | + <li><code>1 <= N <= 10^8</code></li> |
| 44 | + <li>The answer is guaranteed to exist and be less than <code>2 * 10^8</code>.</li> |
| 45 | +</ul> |
| 46 | + |
| 47 | + |
| 48 | +**Related Topics**: |
| 49 | +[Math](https://leetcode.com/tag/math/) |
| 50 | + |
| 51 | +## Solution 1. |
| 52 | + |
| 53 | +```cpp |
| 54 | +// OJ: https://leetcode.com/problems/prime-palindrome/ |
| 55 | +// Author: github.com/lzl124631x |
| 56 | +// Time: O(N) |
| 57 | +// Space: O(logN) |
| 58 | +// Ref: https://leetcode.com/problems/prime-palindrome/solution/ |
| 59 | +class Solution { |
| 60 | + bool isPrime(int x) { |
| 61 | + if (x < 2) return false; |
| 62 | + for (int d = 2, R = sqrt(x); d <= R; ++d) { |
| 63 | + if (x % d == 0) return false; |
| 64 | + } |
| 65 | + return true; |
| 66 | + } |
| 67 | +public: |
| 68 | + int primePalindrome(int N) { |
| 69 | + for (int L = 1; L <= 5; ++L) { |
| 70 | + for (int root = pow(10, L - 1); root < pow(10, L); ++root) { // check for odd-length palindromes |
| 71 | + auto s = to_string(root); |
| 72 | + for (int k = L - 2; k >= 0; --k) s += s[k]; |
| 73 | + int x = stoi(s); |
| 74 | + if (x >= N && isPrime(x)) return x; |
| 75 | + } |
| 76 | + for (int root = pow(10, L - 1); root < pow(10, L); ++root) { |
| 77 | + auto s = to_string(root); |
| 78 | + for (int k = L - 1; k >= 0; --k) s += s[k]; |
| 79 | + int x = stoi(s); |
| 80 | + if (x >= N && isPrime(x)) return x; |
| 81 | + } |
| 82 | + } |
| 83 | + return -1; |
| 84 | + } |
| 85 | +}; |
| 86 | +``` |
| 87 | +
|
| 88 | +## Solution 2. Brute Force with Mathematical Shortcut |
| 89 | +
|
| 90 | +```cpp |
| 91 | +// OJ: https://leetcode.com/problems/prime-palindrome/ |
| 92 | +// Author: github.com/lzl124631x |
| 93 | +// Time: O(N) |
| 94 | +// Space: O(1) |
| 95 | +// Ref: https://leetcode.com/problems/prime-palindrome/solution/ |
| 96 | +class Solution { |
| 97 | + bool isPrime(int x) { |
| 98 | + if (x < 2) return false; |
| 99 | + for (int d = 2, R = sqrt(x); d <= R; ++d) { |
| 100 | + if (x % d == 0) return false; |
| 101 | + } |
| 102 | + return true; |
| 103 | + } |
| 104 | + int reverse(int N) { |
| 105 | + int ans = 0; |
| 106 | + while (N > 0) { |
| 107 | + ans = 10 * ans + (N % 10); |
| 108 | + N /= 10; |
| 109 | + } |
| 110 | + return ans; |
| 111 | + } |
| 112 | +public: |
| 113 | + int primePalindrome(int N) { |
| 114 | + while (true) { |
| 115 | + if (N == reverse(N) && isPrime(N)) return N; |
| 116 | + ++N; |
| 117 | + if (1e7 < N && N < 1e8) N = 1e8; |
| 118 | + } |
| 119 | + } |
| 120 | +}; |
| 121 | +``` |
0 commit comments