javascript solution: 128ms
It\'s tempted to get stuck in javascript when you only focus on how to find a prime quicker, because in that case your code will always time out. The only way t
It\'s tempted to **get stuck** in javascript when you only focus on how to find a prime quicker, because in that case your code will always **time out**. The only way to avoid time out when you focus on prime is to find all the solutions and then **save them as a const** to refer to.(then you\'ll have no fun programming)\n**The right way** to think of this problem is to `find the next palindrome`. There are big gaps between two close palindrome, and we can use those gaps to save time. So the key point of this solution lies in finding the next palindrome based on the palindrome we dealt before.\n```\nvar primePalindrome = function(N) {\n\tif(N<=2) return 2;\n\tlet cur = N;\n\twhile(!isPal(cur)) cur++;\n\twhile(true){\n\t\tif(isPrime(cur)) return cur;\n\t\tcur = nextPal(cur);\n\t}\n};\nfunction isPal(n){\n\treturn (\'\'+n)===(\'\'+n).split(\'\').reverse().join(\'\');\n}\nfunction nextPal(n){\n\tlet s = (\'\'+n), len = s.length, half = Math.ceil(len/2);\n\tlet left=s.slice(0,half);\n\tlet tmp = (+left+1)+\'\';\n\tif(half===len-half){\n\t\treturn +(tmp+tmp.slice(0,half).split(\'\').reverse().join(\'\')); //66->77 99->101\n\t}else{\n\t\tif(tmp.length===half) return +(tmp+tmp.slice(0,len-half).split(\'\').reverse().join(\'\'));// 888->898\n\t\telse return +(tmp.slice(0,half)+tmp.slice(0,half).split(\'\').reverse().join(\'\'));// 999->1001\n\t}\n}\nfunction isPrime(n){\n\tif(n%2===0) return false;\n\tfor(let i=3; i*i<=n; i+=2){\n\t\tif(n%i===0) return false;\n\t}\n\treturn true;\n}\n```\nThe part of my code to find the next palindrome is not elegant. I tried to cut the code but failed. If you have any good idea about making it shorter, please let me know. Thanks `:)`
[]