// CodeEval Prime Palindrome
class prime_palindrome {
public static void main(String[] args) {
int ans = 2;
for (int i = 2; i <= 1000; i++) {
if (IsPrime(i)) {
String s = String.valueOf(i);
String r = new StringBuffer(s).reverse().toString();
if (r.equals(s))
ans = i;
}
}
System.out.println(ans);
}
public static boolean IsPrime(long num) {
if (num < 2)
return false;
if (num == 2 || num == 3)
return true;
if (num % 2 == 0)
return false;
long sqrtnum = (long) Math.sqrt(num) + 1;
for (long x = 3; x <= sqrtnum; x += 2) {
if (num % x == 0)
return false;
}
return true;
}
}
No comments:
Post a Comment