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,...
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....
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...
Python Programming 4th edition: Write a program that asks the user for the number of hours...
Python Programming 4th edition: Write a program that asks the user for the number of hours (float) and the pay rate (float) for employee pay role sheet. The program should display the gross pay with overtime if any and without overtime. Hints: Given base_hr as 40 and ovt_multiplier as1.5, calculate the gross pay with and Without overtime. The output should look like as follows: Enter the number of hours worked: Enter the hourly pay rate: The gross pay is $XXX.XX
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...
A prime number (or prime) is a natural number greater than 1 that has no posítive...
A prime number (or prime) is a natural number greater than 1 that has no posítive divisors other than 1 and itself. Write a Python program which takes a set of positive numbers from the input and returns the sum of the prime numbers in the given set. The sequence will be ended with a negative number.
Python Programming Please! #Name: #Date: #Random number, loop while true #ask user for number. Check to...
Python Programming Please! #Name: #Date: #Random number, loop while true #ask user for number. Check to see if the value is a number between 1 and 10 #if number is too high or too low, tell user, if they guessed it break out of loop Display "Welcome to my Guess the number program!" random mynumber count=1 while True try Display "Guess a number between 1 and 10"   Get guess while guess<1 or guess>10 Display "Guess a number between 1 and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT