classSolution { publicintprimePalindrome(int N) { for (intL=1; L <= 5; ++L) { //Check for odd-length palindromes for (introot= (int)Math.pow(10, L - 1); root < (int)Math.pow(10, L); ++root) { StringBuildersb=newStringBuilder(Integer.toString(root)); for (intk= L-2; k >= 0; --k) sb.append(sb.charAt(k)); intx= Integer.valueOf(sb.toString()); if (x >= N && isPrime(x)) return x; //If we didn't check for even-length palindromes: //return N <= 11 ? min(x, 11) : x }
//Check for even-length palindromes for (introot= (int)Math.pow(10, L - 1); root < (int)Math.pow(10, L); ++root) { StringBuildersb=newStringBuilder(Integer.toString(root)); for (intk= L-1; k >= 0; --k) sb.append(sb.charAt(k)); intx= Integer.valueOf(sb.toString()); if (x >= N && isPrime(x)) return x; } }
thrownull; }
publicbooleanisPrime(int N) { if (N < 2) returnfalse; intR= (int) Math.sqrt(N); for (intd=2; d <= R; ++d) if (N % d == 0) returnfalse; returntrue; } }
classSolution(object): defprimePalindrome(self, N): defis_prime(n): return n > 1andall(n%d for d in xrange(2, int(n**.5) + 1))
for length in xrange(1, 6): #Check for odd-length palindromes for root in xrange(10**(length - 1), 10**length): s = str(root) x = int(s + s[-2::-1]) #eg. s = '123' to x = int('12321') if x >= N and is_prime(x): return x #If we didn't check for even-length palindromes: #return min(x, 11) if N <= 11 else x
#Check for even-length palindromes for root in xrange(10**(length - 1), 10**length): s = str(root) x = int(s + s[-1::-1]) #eg. s = '123' to x = int('123321') if x >= N and is_prime(x): return x
classSolution { publicintprimePalindrome(int N) { while (true) { if (N == reverse(N) && isPrime(N)) return N; N++; if (10_000_000 < N && N < 100_000_000) N = 100_000_000; } }
publicbooleanisPrime(int N) { if (N < 2) returnfalse; intR= (int) Math.sqrt(N); for (intd=2; d <= R; ++d) if (N % d == 0) returnfalse; returntrue; }
publicintreverse(int N) { intans=0; while (N > 0) { ans = 10 * ans + (N % 10); N /= 10; } return ans; } }
[]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution(object): defprimePalindrome(self, N): defis_prime(n): return n > 1andall(n % d for d in xrange(2, int(n**.5) + 1))
defreverse(x): ans = 0 while x: ans = 10 * ans + x % 10 x /= 10 return ans
whileTrue: if N == reverse(N) and is_prime(N): return N N += 1 if10**7 < N < 10**8: N = 10**8