程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python calculation prime learning record

編輯:Python

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} ')
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.

i5-10400 Calculation time :

Montage Jintide(R) C4215R(CentOS The server ,3.2GHz) Calculation time :

i5-6200U Calculation time :


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved