In: Computer Science
A number is a palindromic prime if it is a prime number as well as a palindromic number (ie. it is the same number when the digits are reversed).
For example, 10301 is a palindromic prime.
Write a Python program to ask the user how many palindromic primes they would like to compute, and output the values with a maximum of 10 values per line.
Your program should include the following functions:
isPrime(number) - returns True or False
isPalindrome(number) - returns True or False
reverse(number) - returns number reversed
Sample input/output: How many palindromic primes should be computed?: 25
2 3 5 7 11 101 131 151 181 191
313 353 373 383 727 757 787 797 919 929
10301 10501 10601 11311 11411
Python code screenshot:
Sample Inputs and Outputs:
Python Code to copy:
def isPrime(number): # check if number is prime or not
for i in range(2,number):
if(number % i) == 0: # if number is divisible by i = [2 , number) then it is not prime
return False
return True
def reverse(number): # find reverse of a number
rev = 0
while(number != 0):
rev = rev*10 + number%10
number = number//10
return rev
def isPalindrome(number): # check if number is palindrome or not
if number == reverse(number): # if number equals its reverse then it is palindrome
return True
else:
return False
def main():
n = int(input("How many palindromic primes should be computed?: ")) # take input
i = 2
count = 0 # to make sure that a maximum of 10 number a printed in a line
while n > 0:
if(isPrime(i) and isPalindrome(i)): # check for both condition
print(i,end = " ")
count += 1
if( count == 10 ): # if 10 number get printed, print newline
print("\n")
count = 0
n -= 1
i += 1
main()