Search code examples
pythonpython-3.xpalindrome

How to generate prime palindromes in Python 3


I'm trying to generate the prime palindromes between two integers (given by the user) and simply can't figure out where I'm going wrong. Please can someone explain?

Here's what I've got so far:

#Palindrome test
def palindrome(num):
    str(num) == str(num)[::-1]

#Prime test (excl. 3, 5 & 7 because not palindrome)
def prime(num):
    abs(num)%2 != 0
    abs(num)%3 != 0
    abs(num)%5 != 0
    abs(num)%7 != 0


N = int(input("Enter the start point N: "))
M = int(input("Enter the end point M: "))

for x in range(N, M):
    if palindrome(x) and prime(x):
        print(x)

Alright, as was pointed out - there's more than one issue in the code above. I've tried again and come up with this (below), which manages to print out all the palindromes, but also includes the non-prime numbers. Where am I going wrong?

N = int(input("Enter the start point N: "))
M = int(input("Enter the end point M: "))

def palindrome(num):
    return str(num) == str(num)[::-1]

def prime(x):
    for x in range(N, M+1):
        for y in range(2, x):
            if x%y != 0:
                return True

for z in range(N, M):
    if palindrome(z) and prime(z):
        print(z)

Thanks for all the feedback so far!


Solution

  • I took @Banach code little further. Test for prime is changed to accommodate numbers that are divisible by prime numbers itself. Inspiration from here.

    The prime test now works for much larger numbers as well.

    Demo Code

    #Palindrome test
    def palindrome(num):
        return str(num) == str(num)[::-1]
    
    #Prime Test
    def is_prime(n):
      if n == 2 or n == 3: return True
      if n < 2 or n%2 == 0: return False
      if n < 9: return True
      if n%3 == 0: return False
      r = int(n**0.5)
      f = 5
      while f <= r:    
        if n%f == 0: return False
        if n%(f+2) == 0: return False
        f +=6
      return True
    
    #Get user input
    N = int(input("Enter the start point N: "))
    M = int(input("Enter the end point M: "))
    
    for x in range(N, M):
    
        #Check for palindrome and prime number
        if palindrome(x) and is_prime(x):
    
            #Omitting 2,3,5,7 as per request
            if abs(x)%2 != 0 and abs(x)%3 != 0 and abs(x)%5 != 0 and  abs(x)%7 != 0:
                print(x)
    

    Output

    Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
    Type "copyright", "credits" or "license()" for more information.
    >>> ================================ RESTART ================================
    >>> 
    Enter the start point N: 2
    Enter the end point M: 20000
    11
    101
    131
    151
    181
    191
    313
    353
    373
    383
    727
    757
    787
    797
    919
    929
    10301
    10501
    10601
    11311
    11411
    12421
    12721
    12821
    13331
    13831
    13931
    14341
    14741
    15451
    15551
    16061
    16361
    16561
    16661
    17471
    17971
    18181
    18481
    19391
    19891
    19991
    >>>