定理1:素数大于1并且只能被1和它自身整除
定理2:一个数如果能被它自身之外的素数整除就是合数
定理3:任意数至少都可以分解成两个因子相乘
定理4:一个数如果是素数那么他一定不能被其他小于它的素数整除
--------------------------------------------
注意:由于500万以内素数非常多,我就不打印到屏幕了,只统计有多少个素数(count_number表示)
案例一:
import math
import time
start = time.clock()
lst = []
flag = False
count_outer = 0
count_inner = 0
count_compute1 = 0
count_compute2 = 0
count_number = 0
for x in range(1,5000000,2):
count_outer += 1
if x == 1:
continue
count_compute2 += 1
j = math.ceil(math.sqrt(x)) #注意此处先算出值非常关键,不可写入到下面的for循环中去,否则效率减半
for i in lst:
count_inner += 1
count_compute1 += 1
if x % i == 0:
flag = True
break
if i >= j:
flag = False
break
if not flag:
lst.append(x)
count_number += 1
else:
count_number += 1
end = time.clock()
print('time spent = {}'.format(end - start))
print('count_outer = {} , count_inner = {} , count_compute