Question

In: Computer Science

Python Programming An emirp (prime spelled backward) is a nonpalindromic prime number whose reversal is also...

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.

Solutions

Expert Solution

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.


Related Solutions

Prime Number Determines if a number is prime or not I also need the algorithm and...
Prime Number Determines if a number is prime or not I also need the algorithm and pseudocode in java.
Python question Recall that a prime number is an integer that is only divisible by 1...
Python question Recall that a prime number is an integer that is only divisible by 1 and itself. For example, numbers 2, 3, 5, 7, 13, 19 are prime, whereas 4, 10, 12, 100 are not. Also, recall that factors are the numbers you multiply to get another number. For example, 24 has 8 factors: 1, 2, 3, 4, 6, 8, 12, and 24. As you know, any number can be factorized into several (possibly repeating) prime factors. For instance,...
Python Programming, Could you also show the right indents in Python Shell. Below are the skeleton...
Python Programming, Could you also show the right indents in Python Shell. Below are the skeleton of the program, and the input and output. # Initialize list mylist = [ ] # Set up loop to put menu on screen num = 1 while num != 0:      print(" ")     print(" ")     print("            Menu ")      print ("0 - Quit")     print ("1 - Add item to list")     print ("2 - Pop item off list and print...
A palindrome is a string whose reversal is identical to the string. For example, 110010011 is...
A palindrome is a string whose reversal is identical to the string. For example, 110010011 is a palindrome. So are BOB and KAYAK. How many bit strings of length ?? are palindromes? Explain your solution clearly.
A number is a palindromic prime if it is a prime number as well as a...
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) -...
use Python The assertion that every even number is the sum of two prime numbers is...
use Python The assertion that every even number is the sum of two prime numbers is called Goldbach’s conjecture. You will write a program that asks the user for an integer number, then checks if the integer is even, and finally determines two prime numbers that sum to the user’s entered integer number. Assignment Requirements Three functions are required: get_input(): This function takes no parameters, but will ask the user for an integer number. It will return a valid integer....
(PYTHON) A Prime number is an integer greater than 1 that cannot be formed by multiplying...
(PYTHON) A Prime number is an integer greater than 1 that cannot be formed by multiplying two smaller integer other than 1 and itself. For example, 5 is prime because the only ways of writing it as a product, 1 × 5 or 5 × 1. In this question you will write a program that takes a sequence of integers from the user and display all the prime numbers contained in that sequence. We will separate this question in 2...
Problem 3 Write code in R or Rstudio (Programming) A prime number is an integer greater...
Problem 3 Write code in R or Rstudio (Programming) A prime number is an integer greater than one whose only factors are one and itself. For example, the first ten prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29. A twin prime is a prime that has a prime gap of two. Sometimes the term twin prime is used for a pair of twin primes. For example, the five twin prime pairs are (3, 5),...
[PYTHON] Two natural number p,q are called coprime if there are no common prime factors in...
[PYTHON] Two natural number p,q are called coprime if there are no common prime factors in their prime factorization. E.g. 15 and 20 are not coprime because 5 is a common prime number in their prime factorization, while 20 and 9 are coprime. The Euler’s phi function, φ(n), counts the number of all natural numbers ≤ n which are coprime to n. E.g. φ(9) = 6, as all natural numbers ≤ 9 are 1,2,3,4,5,6,7,8,9. And out of these, the numbers...
USING PYTHON ONLY Part 3a: Prime Number Finder Write a program that prompts the user to...
USING PYTHON ONLY Part 3a: Prime Number Finder Write a program that prompts the user to enter in a postive number. Only accept positive numbers - if the user supplies a negative number or zero you should re-prompt them. Next, determine if the given number is a prime number. A prime number is a number that has no positive divisors other than 1 and itself. For example, 5 is prime because the only numbers that evenly divide into 5 are...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT