Question

In: Computer Science

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) - 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

Solutions

Expert Solution

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()


    


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.
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.
Java program Prime Numbers A prime number is a natural number which has exactly two distinct...
Java program Prime Numbers A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. For example, the first four prime numbers are: 2, 3, 5 and 7. Write a java program which reads a list of N integers and prints the number of prime numbers in the list. Input: The first line contains an integer N, the number of elements in the list. N numbers are given in the following lines. Output:...
isPrime Function. A prime number is a number that is only evenly divisible by itself and...
isPrime Function. A prime number is a number that is only evenly divisible by itself and 1. For example, the number 5 is prime because it can only be evenly divided by 1 and 5. The number 6, however, is not prime because it can be divided evenly by 1, 2, 3, and 6. Write a function named isPrime, which takes an integer as an argument and returns true if the argument is a prime number, or false otherwise. The...
5. A prime number is a number that is only evenly divisible by itself and 1....
5. A prime number is a number that is only evenly divisible by itself and 1. For example, the number 5 is prime because it can only be evenly divided by 1 and 5. The number 6, how‐ ever, is not prime because it can be divided evenly by 1, 2, 3, and 6.   Write a Boolean function named is_prime which takes an integer as an argument and returns true if the argument is a prime number, or false otherwise....
Prime number indicator: In this activity, you will design a circuit that indicates whether a number...
Prime number indicator: In this activity, you will design a circuit that indicates whether a number between 0 through 15 is a prime number or not. A prime number is defined as a number that can be divided only by itself and 1. By definition, the numbers 0 and 1 are not prime. The circuit must have four binary inputs A, B, C, D, and one output, G. The output signal G is true (G = 1) if the input...
L= {w belongs to {0,1}* | the number of 0's is a prime number} . Prove...
L= {w belongs to {0,1}* | the number of 0's is a prime number} . Prove that the language L is not regular.
A palindromic number reads the same both ways (left-to-right and right-to-left). The largest palindrome made from...
A palindromic number reads the same both ways (left-to-right and right-to-left). The largest palindrome made from the product of two 2-digit numbers is 9,009 = 91 × 99. The largest palindrome made from the product of two 3-digit numbers is 906,609 = 913 × 993. The largest palindrome made from the product of two 4-digit numbers is 99,000,099 = 9,901 × 9,999. 1. Write a function IN JAVASCRIPT to find the largest palindrome made from the product of two 7-digit...
A prime number is an integer greater than 1 that is evenlydivisible by only 1...
A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. For example, the number 5 is prime because it can only be evenly divided by 1 and 5. The number 6, however, is not prime because it can be divided by 1, 2, 3, and 6.Write a Boolean function named isPrime, which takes an integer as an argument and returns true if the argument is a prime number, and false otherwise. Demonstrate...
Prove or disprove each of the following statements. (a) There exists a prime number x such...
Prove or disprove each of the following statements. (a) There exists a prime number x such that x + 16 and x + 32 are also prime numbers. (b) ∀a, b, c, m ∈ Z +, if a ≡ b (mod m), then c a ≡ c b (mod m). (c) For any positive odd integer n, 3|n or n 2 ≡ 1 (mod 12). (d) There exist 100 consecutive composite integers.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT