Python求解谷歌高速公路招聘广告：{ 无理数e中前十位连续的素数 }.com

{ 无理数e中前十位连续的素数 }.com

“很聪明"？吴军老师的这句话倒是让人来兴趣了，自己也凑性借助计算机的力量琢磨琢磨下这个证明题。

思路

第一步，在一些权威的数学网站上找到ｅ的小数位数据；

N. J. A. Sloane, Table of 50000 digits of e labeled from 1 to 50000 [based on the ICON Project link below]

第二步，如何判断一个数是素数？

Python代码如下：

``````def isPrime(n: int) -> bool:
if n <= 1:
return False
i = 2
# Make use of symmetry. For example, 2*3=6, 3*2=6
while i * i < n:
if n % i == 0:
return False
i += 1
return True``````

......(6x - 1), 6x, 6x + 1, 2(3x + 1), 3(2x + 1), 2(3x + 2), 6x + 5, 6(x+1)......

``````def isPrime(n: int) -> bool:
if n <= 1:
return False
if n <= 3:
return True
# For case of 2(3x + 1), 3(2x + 1), 2(3x + 2)
if n % 2 == 0 or n % 3 == 0:
return False
# For case of the multiplication of prime numbers
i = 5
while i * i < n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True``````

第三步，for循环e的小数位数据判断第一个10位长的素数。

``````import requests
import re

response = requests.get('https://oeis.org/A001113/b001113.txt')

# Save sequence to a file for later use
out_file = open('digits_of_e.txt', 'w')
print(response.text, file=out_file)

queue = []

container = ''
counter = 0
in_file = open('digits_of_e.txt', 'r')
list_in_file = list(in_file)
for index, line in enumerate(list_in_file):
segments = list_in_file[index:index+10]
# get lines at a batch of 10 lines
for segment in segments:
matchObj = re.match(r'([\d]*) (\d).*', segment, re.I)
counter += 1
if counter <= 10:
container += matchObj.group(2) if matchObj != None else ''
else:
counter = 1
if len(container) == 10:
queue.append(container)
container = matchObj.group(2) if matchObj != None else ''
in_file.close()

print(len(queue)) # 49991 indeed

def isPrime(n: int) -> bool:
# Prime number definition version:
'''
if n <= 1:
return False
i = 2
# Make use of symmetry. For example, 2*3=6, 3*2=6
while i * i < n:
if n % i == 0:
return False
i += 1
return True
'''
# Distribution pattern of prime number version:
if n <= 1:
return False
if n <= 3:
return True
# For case of 2(3x + 1), 3(2x + 1), 2(3x + 2)
if n % 2 == 0 or n % 3 == 0:
return False
# For case of the multiplication of prime numbers
i = 5
while i * i < n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True

result = None
for num in queue:
if isPrime(int(num)):
print(num)
result = num
break

print(result == '7427466391')
print(isPrime(7427466391))``````

结束

First10DigitPrimeFoundInConsecutiveDigitsOfE | Kaggle