Prime Palindrome Program in Java and Python
Prime palindrome program with algorithm, explanation, Java solution and simple Python solution for ISC students.
Question:
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, display all prime palindrome integers in the range. The input range must be between 100 and 3000.
Also display the frequency of prime palindrome integers found.
Algorithm:
Step 1: Start.
Step 2: Accept lower limit m and upper limit n.
Step 3: If m and n are outside the allowed range or m is greater than n, display OUT OF RANGE and stop checking.
Step 4: Initialize frequency count to 0.
Step 5: Repeat for every number num from m to n.
Step 6: Count the factors of num to decide whether it is prime.
Step 7: Reverse the digits of num using modulus 10 and integer division by 10.
Step 8: Compare the reversed number with the original num to decide whether it is a palindrome.
Step 9: If num is both prime and palindrome, display it and increment frequency.
Step 10: After the loop ends, display the frequency.
Step 11: Stop.
Explanation:
The program prints prime palindrome numbers in a given range by separating the two checks into methods. This is a good design because a number must satisfy both conditions: it must be prime and it must read the same forwards and backwards. The main method that prints the result simply sends each number in the range to these two helper methods.
The method isPrime() checks primality by counting factors. It runs a loop from 1 to the number itself and increases count whenever the number is divisible by the loop value. A prime number has exactly two factors, 1 and itself, so the method returns true only when count == 2. Though not the most optimized method, it is straightforward and suitable for tracing at school level.
The method isPalin() reverses the number using digit extraction. The variable copy stores the original number, while the working number is reduced digit by digit. The last digit is extracted using % 10, added to rev by multiplying the previous reverse by 10, and then removed using integer division. After reversal, rev is compared with copy. In the range loop, a number is printed only if both isPrime() and isPalin() return true, and the frequency counter is increased.
Java Program:
/**
* The class PalPrime prints all the Prime Palindrome numbers in the given range
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC 2012 Question 1
*/
import java.util.Scanner;
class PalPrime
{
Scanner sc = new Scanner(System.in);
/* Function isPrime( ) returns 'true' when the number 'x' is Prime and 'false' if it is not. */
boolean isPrime(int x)
{
int count=0;
for(int i=1;i<=x;i++)
{
if(x%i==0)
count++;
}
if(count==2)
return true;
else
return false;
}
/*Function isPalin( ) returns 'true' when 'x' is a Palindrome and 'false' if it is not.*/
boolean isPalin(int x)
{
int rev=0,dig;
int copy=x;
while(x>0)
{
dig=x%10;
rev=rev*10+dig;
x=x/10;
}
if(rev==copy)
return true;
else
return false;
}
/* Function showPalPrime( ) accepts the lower and upper limit, and prints all the PalPrime numbers
in between that range by sending each numbers in the range to both the functions isPrime( ) and isPalin( ) */
public void showPalPrime()
{
int m,n;
int c=0;
System.out.print("Enter the Lower Limit (m) = ");
m=sc.nextInt();
System.out.print("Enter the Upper Limit (n) = ");
n=sc.nextInt();
if(m>=n || m>=3000 || n>=3000) // Checking the range of Limits as given in the question
System.out.println("OUT OF RANGE");
else
{
System.out.println("The Prime Palindrome integers are:");
/* The below for loop generates every number starting from 'm' till 'n' and sends it
to both functions isPalin() and isPrime(), to check whether they are both Palindrome and
prime or not. If yes, then they are printed. */
for(int i=m; i<=n; i++)
{
if(isPrime(i)==true && isPalin(i)==true)
{
if(c==0)
System.out.print(i);
/*The above line is printing the first PalPrime number in order to maintain the sequence
of giving a comma ',' before every next PalPrime number, as is given in the Sample Output.*/
else
System.out.print(", "+i);
c++; //Counting the number of PalPrime numbers by incrementing the counter
}
}
System.out.println("Frequency of Prime Palindrome integers: "+c);
}
}
/* The main method creates an object of PalPrime Class and calls the function showPalPrime( ) */
public static void main(String args[])
{
PalPrime ob=new PalPrime();
ob.showPalPrime();
}
}Equivalent Python Program:
# Read the number and keep any required copy for digit or divisor processing.
# Loops and conditions implement the number-property test step by step.
# Display the result according to the flag/counter/calculated value.
m = int(input("m = "))
n = int(input("n = "))
if m < 100 or n > 3000 or m > n:
print("OUT OF RANGE")
else:
count = 0
print("THE PRIME PALINDROME INTEGERS ARE:")
for num in range(m, n + 1):
factor_count = 0
for i in range(1, num + 1):
if num % i == 0:
factor_count = factor_count + 1
copy = num
rev = 0
while copy > 0:
d = copy % 10
rev = rev * 10 + d
copy = copy // 10
if factor_count == 2 and rev == num:
print(num, end=" ")
count = count + 1
print()
print("FREQUENCY OF PRIME PALINDROME INTEGERS:", count)Output:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.