简体   繁体   English

查找连续有七个“ 7”的所有10位质数-Python

[英]Finding all the 10-digit prime numbers, that have seven “7” in a row - Python

so as it states in the title, I'm trying to generate a list of all the 10-digit prime numbers, that have 7x7 in a row. 因此,正如标题中所述,我正在尝试生成所有10位素数的列表,它们连续有7x7。 More precisely, I mean the numbers that can be written as follows: xxx7777777, xx7777777x, x7777777xx, 7777777xxx. 更确切地说,我的意思是可以写成如下数字:xxx7777777,xx7777777x,x7777777xx,7777777xxx。

My idea is to generate a list of all this numbers, and then check which one of them is prime. 我的想法是生成所有这些数字的列表,然后检查其中哪个是质数。 The code is as follows: 代码如下:

import time
def GeneratingTable():
    A = []
    for i in range (1,10):
        for j in range (0,10):
            for k in range (0,10):
                A.append(i*1000000000+j*100000000+k*10000000+7777777)
    for i in range (1,10):
        for j in range (0,10):
            for k in range (1,10):
                A.append(i*1000000000+j*100000000+77777770+k)
    for i in range (1,10):
        for j in range (0,10):
            for k in range (1,10):
                A.append(i*1000000000+777777700+10*j+k)
    for i in range (0,10):
        for j in range (0,10):
            for k in range (1,10):
                A.append(7777777000+i*100+j*10+k)
    A = list(set(A))   # I want to get rid of duplicats here
    print(len(A))
    return A

def ifPrime(n):  # Maybe I can use more efficient algorithm? 
    Prime = 1
    i = 2
    while i * i <= n:
        if n%i == 0:
            Prime = 0
            break
        i += 2
    if Prime == 1:
        return 1
    else:
        return 0



def HowMany():
    counter = 0
    A = GeneratingTable()
    for i in range (len(A)):
        if ifPrime(A[i]):
            print(A[i])
            counter += 1
    return counter



start = time.clock()
print(HowMany())
end = time.clock()
time = end - start
print(time)

I am sure the number of primes I get this way is to high - it's 2115 and the number of elements in my list A is 3159. Is it a problem with my function "GeneratingTable" or checking whether the number is prime? 我确定我通过这种方式获得的质数很高-它是2115,列表A中的元素数是3159。我的函数“ GeneratingTable”还是检查该数是否为质数是问题吗?

Your function ifPrime thinks that all odd numbers are prime because you start at i=2 and increment it by 2 on each loop. 您的函数ifPrime认为所有奇数都是质数,因为您从i=2开始并在每个循环ifPrime其递增2 You therefore only check even divisors. 因此,您甚至只检查除数。 Change i=2 to i=3 and you'll get 327 results after filtering. i=2更改为i=3 ,过滤后将获得327个结果。 I'm not sure if that's the right answer, but the above is at least part of the problem. 我不确定这是否是正确的答案,但是以上至少是问题的一部分。

your prime function is wrong, it should increase i by 1, not 2, or you're missing some primes. 您的素数函数是错误的,它应将i加1,而不是2,否则您缺少一些素数。

Then you should directly add to a set instead of creating a list when generating table, which saves memory & CPU time (also as Chris commented, you're performing loops starting at 0 or 1, which makes you miss values, I overlooked that in my previous post, now all indices start at 0). 然后,您应该在生成表时直接添加到set而不是创建列表,这样可以节省内存和CPU时间(也正如Chris所评论的那样,您正在执行从0或1开始的循环,这会使您错过值,我忽略了我以前的帖子,现在所有索引都从0开始)。 In that case, you can simplify even more using set comprehension , with a 5 formulas to be able to start indices at 1,0,0 and not forget the 7777770xx ones. 在这种情况下,您可以使用set comprehension进一步简化操作,它具有5个公式,能够从1,0,0开始索引,并且不会忘记7777770xx的索引。

(this was tuned into the right solution with a collaborative effort with BM answer, which is more efficient, but also missed cases at first) (通过与BM答案的协作,这已被调整为正确的解决方案,这样效率更高,但一开始也遗漏了案例)

(also note that hashing integers doesn't cost much CPU time as usually the hash is the integer itself) (还请注意,对整数进行哈希处理不会花费很多CPU时间,因为通常哈希本身就是整数)

The rest seems okay. 其余的看起来还可以。 Here's my modifications: 这是我的修改:

import time

def GeneratingTable():
    A = {v for i in range (1,10) for j in range (0,10) for k in range (0,10)
         for v in [i*1000000000+j*100000000+k*10000000+7777777,i*1000000000+j*100000000+77777770+k,i*1000000000+777777700+10*j+k,7777777000+i*100+j*10+k,7777777000+j*10+k]}
    print(len(A))
    return A


def ifPrime(n):
    i = 2
    while i * i <= n:
        if n%i == 0:
            return False
        i += 1
    return True

def check():
    return sorted([p for p in GeneratingTable() if ifPrime(p)])


start = time.clock()
x = check()
print(len(x),x)
end = time.clock()
time = end - start
print(time)

result: 结果:

3420 203 [1247777777, 1277777771, 1277777773, 1457777777, 1487777777, 1577777771, 1577777777, 1657777777, 1777777741, 1777777751, 1777777777, 1787777777, 1877777773, 1877777777, 1877777779, 1927777777, 1957777777, 2017777777, 2027777777, 2077777771, 2377777771, 2437777777, 2467777777, 2507777777, 2567777777, 2647777777, 2677777771, 2777777707, 2777777711, 2777777719, 2777777741, 2777777759, 2777777777, 2777777797, 2917777777, 3037777777, 3077777777, 3137777777, 3197777777, 3247777777, 3257777777, 3377777773, 3377777779, 3407777777, 3427777777, 3527777777, 3557777777, 3577777771, 3777777701, 3777777767, 3777777793, 3827777777, 3937777777, 3977777773, 3977777777, 4027777777, 4097777777, 4177777771, 4277777773, 4297777777, 4307777777, 4327777777, 4447777777, 4567777777, 4687777777, 4747777777, 4777777703, 4777777717, 4777777727, 4777777729, 4777777759, 4777777769, 4777777789, 4777777793, 4777777799, 4867777777, 4937777777, 4997777777, 5177777777, 5237777777, 5387777777, 5477777777, 552777 3420 203 [1247777777,1277777771,1277777773,1457777777,1487777777,1577777771,1577777777,1657777777,1777777741,1777777751,1777777777,1787777777,1877777773,1877777777,1877777779,1927777777777,1957777777,2777777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,177,777,777 ,2567777777、2647777777、2677777771、2777777707、2777777711、2777777719、2777777741、2777777759、2777777777、2777777797、2917777777、3037777777、3077777777、3137777777、3197777777、3247777777、3257777777、3377777773、3377777、7773777777 ,3777777767、3777777793、3827777777、3937777777、3977777773、3977777777、4027777777、4097777777、4177777771、4277777773、4297777777、4307777777、4327777777、4447777777、4567777777、4687777777、4747777777、4777777703、4777777777、4777777777、4777777777、4777777777、4777777777 ,4777777799、4867777777、4937777777、4997777777、5177777777、5237777777、5387777777、5477777777、552777 7777, 5567777777, 5617777777, 5627777777, 5647777777, 5777777701, 5777777771, 5777777791, 5877777779, 6037777777, 6077777773, 6077777777, 6177777773, 6277777777, 6317777777, 6577777771, 6577777777, 6637777777, 6757777777, 6767777777, 6777777731, 6777777737, 6777777757, 6777777791, 6847777777, 6857777777, 6947777777, 6977777771, 6977777773, 7037777777, 7087777777, 7327777777, 7387777777, 7487777777, 7537777777, 7547777777, 7597777777, 7607777777, 7727777777, 7777777019, 7777777027, 7777777057, 7777777069, 7777777081, 7777777103, 7777777127, 7777777169, 7777777199, 7777777207, 7777777211, 7777777229, 7777777237, 7777777261, 7777777327, 7777777361, 7777777369, 7777777379, 7777777391, 7777777421, 7777777429, 7777777453, 7777777493, 7777777517, 7777777549, 7777777577, 7777777597, 7777777633, 7777777639, 7777777649, 7777777663, 7777777669, 7777777691, 7777777703, 7777777741, 7777777781, 7777777783, 7777777789, 7777777823, 7777777849, 7777777853, 7777777871, 7777777937, 7777777963, 7777777993 7777,5567777777,5617777777,5627777777,5647777777,5777777701,5777777771,5777777791,5877777779,6​​037777777,6077777773,6077777777,6177777773,6277777777,6317777777,6577777771,6577777777,6637777777,6757777777,6777777777,6777777777,6777777777 6857777777,6947777777,6977777771,6977777773,7037777777,7087777777,7327777777,7387777777,7487777777,7537777777,7547777777,7597777777,7607777777,7727777777,7777777019,7777777027,7777777057,7777777069,7777777777,7777777777,7777777777,7777777777 7777777229、7777777237、7777777261、7777777327、7777777361、7777777369、7777777379、7777777391、7777777421、7777777429、7777777453、7777777493、7777777517、7777777549、7777777577、7777777597、7777777633、7777777639、7777777777、7777773777 7777777783、7777777789、7777777823、7777777849、7777777853、7777777871、7777777937、7777777963、7777777993 , 7837777777, 7957777777, 8087777777, 8117777777, 8227777777, 8277777773, 8347777777, 8387777777, 8477777771, 8577777773, 8627777777, 8737777777, 8777777713, 8777777717, 8777777759, 8777777777, 8807777777, 8947777777, 8977777777, 9067777777, 9137777777, 9177777773, 9197777777, 9257777777, 9467777777, 9477777773, 9477777779, 9547777777, 9617777777, 9677777771, 9777777767, 9777777787, 9777777799, 9817777777, 9887777777, 9937777777, 9977777773] ,7837777777,7957777777,8087777777,8117777777,8227777777,8277777773,8347777777,8387777777,8477777771,8577777773,8627777777,8737777777,8777777713,8777777717,8777777759,8777777777,8807777777,8947777777,8977777777777 7777777777 ,9477777773、9477777779、9547777777、9617777777、9677777771、9777777767、9777777787、9777777799、9817777777、9887777777、9937777777、9999777773]

note: about the efficiency of the isPrime function: the function is efficient to test 1 prime, but when you have 3000+ primes to test, it's best to pre-compute a prime list up to sqrt(10**10) (for instance using a sieve) and test against those primes. 注意:关于isPrime函数的效率:该函数可以有效地测试1个素数,但是当您要测试3000个以上的素数时,最好预先计算一个最大为sqrt(10**10)的素数列表(例如使用筛子)并针对这些素数进行测试。 Computing the list of primes is an effort, but well made up when testing a lot of primes (like in BM answer) 计算素数列表是一项工作,但是在测试许多素数时(例如BM答案中)就可以很好地完成计算

A approach with efficient tools, and Eratosthene Sieve for better complexity : 带有高效工具的方法以及Eratosthene Sieve可以改善复杂性:

from itertools import *

def p77():
    S=set()
    for triple in combinations_with_replacement('0123456789',3):
        quadruple=triple+('7777777',)
        for perm in permutations(quadruple):
            if perm[0]!='0': # ensure 10 digits 
                s=''.join(perm)
                S.add(int(s))

    A=np.array(list(S))       

    [eratosthene][1]=np.arange(10**5)  # sqrt(10**10)                                      
    for i in range(2,317): # sqrt(10**5)
        if eratosthene[i]>0:
            eratosthene[i*i::i]=0

    little_primes=eratosthene[eratosthene>1]                        
    for p in little_primes:
        A=A[A%p>0]

    return A

This gives 203 primes in 0.1 second : ( -- for 7777777 ) 这在0.1秒内给出203个素数:(-对于7777777)

['124--, 12--1, 12--3, 145--, 148--, 15--1, 15--7, ',
 '165--, 1--41, 1--51, 1--77, 178--, 18--3, 18--7, ',
 '18--9, 192--, 195--, 201--, 202--, 20--1, 23--1, ',
 '243--, 246--, 250--, 256--, 264--, 26--1, 2--07, ',
 '2--11, 2--19, 2--41, 2--59, 2--77, 2--97, 291--, ',
 '303--, 30--7, 313--, 319--, 324--, 325--, 33--3, ',
 '33--9, 340--, 342--, 352--, 355--, 35--1, 3--01, ',
 '3--67, 3--93, 382--, 393--, 39--3, 39--7, 402--, ',
 '409--, 41--1, 42--3, 429--, 430--, 432--, 444--, ',
 '456--, 468--, 474--, 4--03, 4--17, 4--27, 4--29, ',
 '4--59, 4--69, 4--89, 4--93, 4--99, 486--, 493--, ',
 '499--, 51--7, 523--, 538--, 54--7, 552--, 556--, ',
 '561--, 562--, 564--, 5--01, 5--71, 5--91, 58--9, ',
 '603--, 60--3, 60--7, 61--3, 62--7, 631--, 65--1, ',
 '65--7, 663--, 675--, 676--, 6--31, 6--37, 6--57, ',
 '6--91, 684--, 685--, 694--, 69--1, 69--3, 703--, ',
 '708--, 732--, 738--, 748--, 753--, 754--, 759--, ',
 '760--, 772--, --019, --027, --057, --069, --081, ',
 '--103, --127, --169, --199, --207, --211, --229, ',
 '--237, --261, --327, --361, --369, --379, --391, ',
 '--421, --429, --453, --493, --517, --549, --577, ',
 '--597, --633, --639, --649, --663, --669, --691, ',
 '--703, --741, --781, --783, --789, --823, --849, ',
 '--853, --871, --937, --963, --993, 783--, 795--, ',
 '808--, 811--, 822--, 82--3, 834--, 838--, 84--1, ',
 '85--3, 862--, 873--, 8--13, 8--17, 8--59, 8--77, ',
 '880--, 894--, 89--7, 906--, 913--, 91--3, 919--, ',
 '925--, 946--, 94--3, 94--9, 954--, 961--, 96--1, ',
 '9--67, 9--87, 9--99, 981--, 988--, 993--, 99--3   ]

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

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