Search code examples
pythonfunctionloopsprimespalindrome

Find palindromic primes in Python


A palindromic prime is a prime number that is also palindromic. For example, 131 is a prime and also a palindromic prime, as are 313 and 757.

I need to write a function that displays the first n palindromic prime numbers. Display 10 numbers per line and align the numbers properly, as follows:

2     3     5     7    11   101   131   151   181   191
313   353   373   383   727   757   787   797   919   929

my code is:

def paliPrime(n):
    a=0
    b=n
    a+=1
    for i in range(a,b):
        paliPrime=True
        if str(i) == str(i)[::-1]:
            if i>2:
                for a in range(2,i):
                    if i%a==0:
                        paliPrime=False
                        break
                if paliPrime:
                    print i

the code works but not in the way i wanted:

>>> paliPrime(10)
3
5
7
>>> 

And what I want is a function that displays the first n palindromic prime numbers. It should display 10 numbers per line and align the numbers properly.


Solution

  • WillNess showed you a really good way of doing (go with it). I'll show you what you did wrong with your way so you can learn from it.

    Since you don't know the range of the first N prime palidromes, you want to iterate indefinitely and keep a count of those you have found. In simplified pseudocode.

    count = 0
    number = 2
    while count < N
        if number is palidromic prime
            print number
            count += 1
        number += 1
    

    With adding some bells and whistles in your code to print the numbers in the right format, you get

    def paliPrime(n):
        fmt = '%-5d'
        if n >= 1:
            print fmt % 2,
        count = 2
        i = 3
        while count <= n:
            paliPrime=True
            if str(i) == str(i)[::-1]:
                for a in range(2,i):
                    if i%a==0:
                        paliPrime=False
                        break
                if paliPrime:
                    print fmt % i,
                    if count%10 == 0:
                        print 
                    count += 1
            i += 2
        # add a newline at the end if we haven't done so already
        if count%10 != 1:
            print
    

    A bit of general advice is that you should keep each function to one responsibility. Here you both generate and print the numbers. Imagine if one day you wanted to reuse the code to generate these numbers so they could be silently used in your program. You'd be littered with prints everywhere.

    Now, regarding the solution, you may have noticed is that I investigated numbers starting from 3 and in increments of 2. That's because you're guaranteed all even numbers, except from 2, to not be prime.

    And here what WillNess showed you becomes relevant. There are much better algorithms to generate the next prime or check whether a number is prime instead of brute forcing trial division, which, by the way, you can limit up to sqrt(i).