python arithmetic Prime Number

Arithmetic Primes

1. Problem Description:
Similar 7,37,67,97,107,137,167,197, so that the number of columns consisting of a prime number called arithmetic Prime Number.
Prime Number limit having term generally refers to a number of prime numbers is the number of columns the number of consecutive items, how many consecutive entries may be present up.
2. Title:
Programming arithmetic to find prime numbers less than 100 the number of columns.

Problem-solving ideas:

# 1, sieve find all primes
# 2 for both two compositions, constructor known arithmetic the prime list column a0, a1 item
# 3. Calculated a2, look-up table is determined a2 whether the prime number is a prime number can be constituted prime arithmetic sequence, calculating A3 ...

n=int(input('请输入查找范围:'))
def primeNumber(n):
    pt=[True]*n
    s=[]
    for p in range(2,n):
        if not pt[p]:
            continue
        s.append(p)
        for i in range(p*p,n,p):
            pt[i]=False
    return pt,s
pt,s=primeNumber(n)

for i in range(len(s)):
    for j in range(i+1,len(s)):
        a0,a1=s[i],s[j]
        an = a1 + a1 - a0 #公差相等,前后项相加,等于中间项的2倍
        k = []
        while an < n and pt[an]: #判断an项是否为素数,如果是则添加到k,打印出来
            k.append(an)
            an += a1 - a0   #
        if k:
            print ([a0, a1] +k)
Published 46 original articles · won praise 37 · views 4548

Guess you like

Origin blog.csdn.net/weixin_42444693/article/details/103534737