Calculate prime numbers in a certain range , And recorded in json In file . The next calculation can be based on the last iteration
In from 3 In the case of starting to calculate prime numbers , Calculation 100 Prime numbers within ten thousand can be 1 seconds
import json
import time
start = time. time()
limit = 1e6
try:
with open ( 'prime.json', 'r') as file:
list = json. load( file)
num = list[ - 1]
except FileNotFoundError:
list = [ 3]
num = 3
def check( num):
for i in list:
if num % i == 0:
break
elif i * i > num: # Don't write here i**2, The calculation speed will be slow 2 More than double
list. append( num)
#print(num)
break
while num < limit:
num = num + 2
check( num)
# with open ('prime.json', 'w') as file:
# json.dump(list,file,indent=4)
print( list[ - 1])
end = time. time()
print( f' Time for this calculation :{ end - start} ')
i5-10400 Calculation time :
Montage Jintide(R) C4215R(CentOS The server ,3.2GHz) Calculation time :
i5-6200U Calculation time :