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

梦想的初衷 提交于 2021-01-27 20:25:27

问题


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. More precisely, I mean the numbers that can be written as follows: 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?


回答1:


your prime function is wrong, it should increase i by 1, not 2, or you're missing some primes.

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). 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.

(this was tuned into the right solution with a collaborative effort with B.M. answer, which is more efficient, but also missed cases at first)

(also note that hashing integers doesn't cost much CPU time as usually the hash is the integer itself)

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, 5527777777, 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, 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]

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. Computing the list of primes is an effort, but well made up when testing a lot of primes (like in B.M answer)




回答2:


Your function ifPrime thinks that all odd numbers are prime because you start at i=2 and increment it by 2 on each loop. You therefore only check even divisors. Change i=2 to i=3 and you'll get 327 results after filtering. I'm not sure if that's the right answer, but the above is at least part of the problem.




回答3:


A approach with efficient tools, and Eratosthene Sieve for better complexity :

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 )

['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   ]


来源:https://stackoverflow.com/questions/47005608/finding-all-the-10-digit-prime-numbers-that-have-seven-7-in-a-row-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!