Categories

# Prime Palindrome ISC 2012 Practical

A prime palindrome integer is a positive integer (without leading zeroes) which is prime as well as a palindrome. Given two positive integers m and n, where m < n, write a program to determine how many prime palindrome integers are there in the range between m and n (both inclusive) and output them.

The input contains two positive integers m and n where m < 3000 and n < 3000. Display the number of prime palindrome integers in the specified range along with their values in the format specified below:

Test your program with the sample data and some random data:

Example 1:
INPUT:
m = 100
n = 1000
OUTPUT:
THE PRIME PALINDROME INTEGERS ARE:
101, 131, 151, 181, 191, 313, 353, 373, 383, 727, 757, 787, 919, 929
FREQUENCY OF PRIME PALINDROME INTEGERS: 15

Example 2:
INPUT:
m = 100
n = 5000
OUTPUT:
OUT OF RANGE.

``````import java.io.*;
class PrimePalindrome{
public static void main(String args[])throws IOException{
System.out.print("m = ");
System.out.print("n = ");
if(m > 3000 || n > 3000 || m > n){
System.out.println("OUT OF RANGE.");
return;
}
int count = 0;
System.out.println("THE PRIME PALINDROME INTEGERS ARE:");
for(int i = m; i <= n; i++){
if(isPalindrome(i) && isPrime(i)){
if(count == 0)
System.out.print(i);
else
System.out.print(", " + i); count++;
}
}
System.out.println();
System.out.println("FREQUENCY OF PRIME PALINDROME INTEGERS: " + count);
}
public static boolean isPalindrome(int n){
int rev = 0;
for(int i = n; i > 0; i /= 10)
rev = rev * 10 + i % 10;
return n == rev;
}
public static boolean isPrime(int n){
int f = 0;
for(int i = 1; i <= n; i++){
if(n % i == 0)
f++;
if(f > 2)
return false;
}
return f == 2;
}
}``````