Upside Down Primes
Jump to navigation
Jump to search
Check whether the number is prime by iterating up to sqrt(n) and seeing if any number %2 == 0. Flip the number by converting to a string, exchanging characters as appropriate, and reversing the string. Check whether that converted number is prime. In java, this can be done in 1 line.
import java.util.*;
public class k {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
long n=in.nextLong();
System.out.println((isprime(n)&&isprime(reverse(n))&&n!=1?"yes":"no"));
}
private static long reverse(long n) {
return Long.parseLong(new StringBuilder(Long.toString(n).replaceAll(".*[347].*","4").replaceAll("6", "a").replaceAll("9", "6").replaceAll("a", "9")).reverse().toString());
}
private static boolean isprime(long n) {
long top=(long)Math.ceil(Math.sqrt(n));
for(int i=2;i<=top&&i<n;i++)if(n%i==0)return false;
return true;
}
}