{"id":3013,"date":"2018-07-08T01:23:09","date_gmt":"2018-07-08T08:23:09","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3013"},"modified":"2018-07-08T01:29:20","modified_gmt":"2018-07-08T08:29:20","slug":"leetcode-867-prime-palindrome","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-867-prime-palindrome\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 867. Prime Palindrome"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Find the smallest prime palindrome greater than or equal to\u00a0<code>N<\/code>.<\/p>\n<p>Recall that a\u00a0number is\u00a0<em>prime<\/em>\u00a0if it&#8217;s only divisors are 1 and itself, and it is greater than 1.<\/p>\n<p>For example, 2,3,5,7,11 and 13 are\u00a0primes.<\/p>\n<p>Recall that a number is a\u00a0<em>palindrome<\/em>\u00a0if it reads the same from left to right as it does from right to left.<\/p>\n<p>For example, 12321 is a palindrome.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-1-1\">6<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">7<\/span>\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-2-1\">8<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">11<\/span>\r\n<\/pre>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-3-1\">13<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-3\">101<\/span><\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li><code>1 &lt;= N &lt;= 10^8<\/code><\/li>\n<li>The answer is guaranteed to exist and be less than\u00a0<code>2 * 10^8<\/code>.<\/li>\n<\/ul>\n<h1><strong>Solution: Math<\/strong><\/h1>\n<p>All odd digits palindromes have a factor 11, they are not prime except 11 itself.<\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 8 ms\r\nclass Solution {\r\npublic:\r\n  int primePalindrome(int N) {\r\n    if (N &lt;= 2) return 2;\r\n    if (N % 2 == 0) ++N;\r\n    while (true) {      \r\n      if (reverse(N) == N &amp;&amp; isPrime(N)) \r\n        return N;\r\n      N += 2;\r\n      if (N &gt; 11 &amp; (int)log10(N) % 2 == 1)\r\n        \/\/ 13 -&gt; 101\r\n        \/\/ 1001 -&gt; 10001                \r\n        N = pow(10, (int)log10(N) + 1) + 1;\r\n    }\r\n    return -1;\r\n  }\r\nprivate:\r\n  int reverse(int n) {\r\n    int r = 0;\r\n    while (n) {\r\n      r = 10 * r + n % 10;\r\n      n \/= 10;\r\n    }\r\n    return r;\r\n  }\r\n  \r\n  bool isPrime(int n) {\r\n    if (n &lt;= 1) return false;\r\n    for (int i = 2; i &lt;= sqrt(n); ++i)\r\n      if (n % i == 0) return false;\r\n    return true;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Find the smallest prime palindrome greater than or equal to\u00a0N. Recall that a\u00a0number is\u00a0prime\u00a0if it&#8217;s only divisors are 1 and itself, and it is&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[31,95,59],"class_list":["post-3013","post","type-post","status-publish","format-standard","hentry","category-math","tag-math","tag-palindrome","tag-prime","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3013","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=3013"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3013\/revisions"}],"predecessor-version":[{"id":3017,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3013\/revisions\/3017"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}