简体   繁体   English

python中的回文质数

[英]Palindromic prime number in python

So I'm trying to figure out how to find all the palindrome prime numbers between 2 numbers.所以我想弄清楚如何找到 2 个数字之间的所有回文素数。 So far my code can find the palindrome but when I check for a prime number, it prints out the non-primes as well.到目前为止,我的代码可以找到回文,但是当我检查质数时,它也会打印出非质数。 And there are numbers which are printed multiple times.并且有多次打印的数字。

Could you please help.能否请你帮忙。

Thanks.谢谢。

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

Based on your most recent code, you simply need to make sure that you reset y , which serves as your positive indicator of primality, for each number you test. 根据您最近的代码,您只需要确保对每个测试的数字重置y作为素数的肯定指示符。 Otherwise, it will stay False when you get to the number 4, the first composite number. 否则,当您到达第一个复合数字4时,它将保持为False

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


3
5
7
11
101
131
151
181
191
313
353
373
383

As you can see, all of these are prime. 如您所见,所有这些都是主要的。 You can check the list of primes wolframalpha provides to be sure that no palindromic primes have been omitted. 您可以检查Wolframalpha提供的素数列表,以确保没有遗漏回文素数。 If you want to include 2, add a special case for that. 如果要包括2,请为此添加一个特殊情况。

Please see my comments below: 请在下面查看我的评论:

a = 0
b = 500
a += 1
y = True
for i in range(a,b):
    if(str(i) == str(i)[::-1]):
        print (i)      # <--- You print every number that is a palindrome
        if(i>2):
            for a in range(2,i):
                if(i%a==0):
                    y = False   # <--- This never gets set back to True
                    break
            if y:
                print(i)

        i+=i   # <--- This is doing nothing useful, because it's a "for" loop

Look at my code below, we don't need to initialize Y as well. 看下面的代码,我们也不需要初始化Y。 A For-Else block works well. For-Else块效果很好。

a = 0
b = 500
a += 1
for i in range(a,b):
    if(str(i) == str(i)[::-1]):
        if(i>1):
            for a in range(2,i):
                if(i%a==0):
                    y = False
                    break
            else:
                print(i)

To get 2 included in the answer, just be sure to check the if condition as (i>1) as mentioned by @elias 要在答案中包含2,只需确保将@elias提到的if条件检查为(i> 1)

Here is the pretty fast implementation based on "Sieve of Atkin" algorithm.这是基于“阿特金筛法”算法的非常快速的实现。 I calculate all primes numbers up to the end and then filter only palindromic ones where number is greater or equal to start.我计算所有素数直到最后,然后仅过滤数字大于或等于开始的回文数。

import math
import sys

def pal_primes(start,end):
    return list(filter(lambda x: str(x)==str(x)[::-1] and x>=start, sieveOfAtkin(end)))
    
    def sieveOfAtkin(limit):
        P = [1,2,3]
        sql = int(math.sqrt(limit))
        r = range(1,sql+1)
        sieve=[False]*(limit+1)
        for x in r:
            for y in r:
                xx=x*x
                yy=y*y
                xx3 = 3*xx
                n = 4*xx + yy
                if n<=limit and (n%12==1 or n%12==5) : sieve[n] = not sieve[n]
                n = xx3 + yy
                if n<=limit and n%12==7 : sieve[n] = not sieve[n]
                n = xx3 - yy
                if x>y and n<=limit and n%12==11 : sieve[n] = not sieve[n]
        for x in range(5,sql):
            if sieve[x]:
                xx=x*x
                for y in range(xx,limit+1,xx):
                    sieve[y] = False
        for p in range(5,limit):
            if sieve[p] : P.append(p)       
        return P
    
    
    if __name__=="__main__":
        print(pal_primes(int(sys.argv[1]),int(sys.argv[2])))

Based on this thread:基于这个线程:

Sieve Of Atkin Implementation in Python 阿特金筛法在 Python 中的实施

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM