# 质数

1、素数的个数总是按照自然数增加10倍来统计展现的。因为这里一直沿用π(x)与x/lnx的统计方法。
2、100以内有25个素数，1000以内有168个素数。就产生了一种根深蒂固的观念：素数越来越稀疏。

200亿以内有90个；200-400亿之间有55个；

400-600亿之间有41个；
600-800亿之间有41个；
800-1000亿之间有40个；

## 打印质数

### 打印100以内的质数

``````count = 0
for i in range(2,101):
for x in range(2,i):
if i%x == 0:
break
else:
print(i)
count += 1
print("\n","Total: ",count,"number")

----
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97

Total:  25 number``````

### 优化算法，寻找中点

``````count = 0
for i in range(2,100000):
for x in range(2,int(i**0.5)+1):
if i%x == 0:
break
else:
print(i)
count += 1
print("\n","Total: ",count,"number")

----
2
3
5
7
11
13
·······
99907
99923
99929
99961
99971
99989
99991

Total:  9592 number``````

### 继续优化，大于2的偶数都不是质数

``````count = 1
print(2)
for i in range(3,100000,2):
for x in range(2,int(i**0.5)+1):
if i%x == 0:
break
else:
print(i)
count += 1
print("\n","Total: ",count,"number")

----
2
3
5
7
11
13
·······
99907
99923
99929
99961
99971
99989
99991

Total:  9592 number``````

### 进行优化，第二层的for循环

``````count = 1
print(2)
for i in range(3,100000,2):
for x in range(3,int(i**0.5)+1,2):
if i%x == 0:
break
else:
print(i)
count += 1
print("\n","Total: ",count,"number")

----
2
3
5
7
11
13
·······
99907
99923
99929
99961
99971
99989
99991

Total:  9592 number``````

## 计算以上方法的效率

### 方法一

``````from datetime import datetime
t1 = datetime.now()
count = 0
for i in range(2,1000000):
for x in range(2,i):
if i%x == 0:
break
else:
#print(i)
count += 1
print("\n","Total: ",count,"number")
t2 = datetime.now()
print("Total_Cost:",(t2-t1).total_seconds(),"s")``````

### 方法二

``````from datetime import datetime
t1 = datetime.now()
count = 0
for i in range(2,1000000):
for x in range(2,int(i**0.5)+1):
if i%x == 0:
break
else:
#print(i)
count += 1
print("\n","Total: ",count,"number")
t2 = datetime.now()
print("Total_Cost:",(t2-t1).total_seconds(),"s")

----
Total:  78498 number
Total_Cost: 6.26467 s``````

### 方法三

``````from datetime import datetime
t1 = datetime.now()
count = 1
#print(2)
for i in range(3,1000000,2):
for x in range(2,int(i**0.5)+1):
if i%x == 0:
break
else:
# print(i)
count += 1
print("\n","Total: ",count,"number")

t2 = datetime.now()
print("Total_Cost:",(t2-t1).total_seconds(),"s")

----
Total:  78498 number
Total_Cost: 5.80345 s``````

### 方法四

``````from datetime import datetime
t1 = datetime.now()
count = 1
#print(2)
for i in range(3,1000000,2):
for x in range(3,int(i**0.5)+1,2):
if i%x == 0:
break
else:
#print(i)
count += 1
print("\n","Total: ",count,"number")

t2 = datetime.now()
print("Total_Cost:",(t2-t1).total_seconds(),"s")

----
Total:  78498 number
Total_Cost: 3.375002 s``````

## 进阶方法

### 在上述方法四的基础上，引入列表

#### 先把第二层循环用列表替代

``````from datetime import datetime
t1 = datetime.now()
count = 1
lst = [2]
for i in range(3,1000000,2):
#for x in range(3,int(i**0.5)+1,2):
for x in lst:
if i%x == 0 and x <= i**0.5:
break
else:
lst.append(i)
count += 1
t2 = datetime.now()
print("Total Number:", count, "Total_Cost:", (t2-t1).total_seconds(),"s")

----
Total Number: 78498   Total_Cost: 234.643142 s``````

#### 修改if and 结构

``````from datetime import datetime

t1 = datetime.now()
count = 1
lst = [2]
for i in range(3,1000000,2):
flag = False
middle = int(i**0.5)
for x in lst:
if i%x == 0:
break
if x > middle:
flag = True
break
if flag:
lst.append(i)
count += 1
t2 = datetime.now()
print("Total Number:", count, "Total_Cost:", (t2-t1).total_seconds(),"s")

----
Total Number: 78498 Total_Cost: 1.560107 s``````

#### 还能优化吗？

``````from datetime import datetime

t1 = datetime.now()
count = 2 #[2]
lst = [3]
for i in range(5,1000000,2):
flag = False
middle = int(i**0.5)
for x in lst:
if i%x == 0:
break
if x > middle:
flag = True
break
if flag:
lst.append(i)
count += 1
t2 = datetime.now()
print("Total Number:", count, "Total_Cost:", (t2-t1).total_seconds(),"s")

----
Total Number: 78498 Total_Cost: 1.513069 s``````

### 孪生素数

``````from datetime import datetime

t1 = datetime.now()

count = 3 #2,3,5
lst = [3,5]
step = 4
i = 7
while i < 1000000:
if i%5 != 0:
middle = int(i**0.5)
flag = False
for x in lst:
if i%x == 0:
break
if x > middle:
flag = True
break
if flag:
lst.append(i)
count += 1

i += step
step = 4 if step == 2 else 2

t2 = datetime.now()
print("Total Number:", count, "Total_Cost:", (t2-t1).total_seconds(),"s")

----
Total Number: 78498 Total_Cost: 1.35155 s``````

`1.513069s` —> `1.35155s` 还可以哈哈

## 质数相关的理论

• 哥德巴赫猜想：任何大于5的奇数都是三个素数之和
• 衍生：任何一个大于2的偶数都是两个素数之和
• 伯特兰猜想：对于任意正整数n>1，存在一个素数p，使得n<p<2n
• 孪生素数猜想：存在无穷多的形如p和p+2的素数对
• n2+1 猜想：存在无穷多个形如n2+1的素数，其中n是正整数