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

A palindromic number is a number that remains the same when its digits are reversed. For...
A palindromic number is a number that remains the same when its digits are reversed. For examples, 1, 11, 99, 121 are palindromic. Write a program that takes one array of 10 positive integers as input and output all the palindromic numbers in this array. We assume that each input number is valid, i.e., a positive integer. Hint: You can consider the following steps to check whether a number (e.g., 121) is palindromic • Store this number to one variable,...
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.
A Palindromic number is one that reads the same backwards and forwards. Write a MATLAB function...
A Palindromic number is one that reads the same backwards and forwards. Write a MATLAB function (call it palin.m) that takes as input a positive integer, and returns 1 (true) if it is palindromic, 0 (false) if it is not.  
A palindrome prime is a prime number that reads the same forwards or backwards. An example...
A palindrome prime is a prime number that reads the same forwards or backwards. An example of a palindrome prime is 131. Write a method with the following signature for determining if a given number is a palindrome prime. public static boolean isPallyPrime(int nVal) Note: For this assignment you are not allowed to use the built in Java class Array as part of your solution for any of these questions. Your Method signatures must be the same as given here.
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:...
A prime number is a number that is only evenly divisible by itself and 1. For...
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. Design a Boolean function called isPrime, that accepts an integer as an argument and returns True if the argument is a prime number, or False otherwise. Use the...
A prime number is a number that is only evenly divisible by itself and 1. For...
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 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. Use the...
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....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT