In: Computer Science
Python Programming
An emirp (prime spelled backward) is a nonpalindromic prime number whose reversal is also a prime. For example, 17 and 71 are prime numbers, so 17 and 71 are emirps. Write a program that displays the first 10 emirps.
Please refer to the screenshot of the code to
understand the indentation of the code
CODE:
#this code is in Python 3
import math
#this function will check if a number is prime or not.
def isPrime(n):
#eliminate multiples of 2 and 3
if ((n % 2 == 0) or (n % 3 == 0)):
return 0
#checking numbers for multiples of 5 or more.
else:
for i in range(5,math.ceil(math.sqrt(n)+1),6):
if ((n % i == 0) or (n % (i + 2) == 0)):
return 0
return 1
#this function will check the reverse of the number to be non
palindromic and prime.
def isEmirp(n):
s1 = str(n)
s2 = s1[::-1]
#check for palindrome
if(s1 == s2):
return 0
else:
#check for prime
if(isPrime(int(s2))):
return 1
#counter for loop
count = 0
#all primes below 12 are palindromic
n = 12
#driver function to print 10 Emirps
while(count < 10):
if(isPrime(n) == 1):
if(isEmirp(n) == 1):
print(n)
n += 1
count += 1
else:
n += 1
else:
n += 1
-----------------------------------------------------------------------------------------------------------------------------
Working hard on improving the quality of solutions I deliver. A
Thumbs Up is appreciated. Thanks!
For queries leave a comment, I will be most happy to
help.