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

Python solves prime numbers below 100 million

編輯:Python

How many primes are there below 100 million ?

be prompted by a sudden impulse , Wrote a code to solve prime numbers below 100 million , To test the computing speed of the notebook .
Finally, it took more than two hours .
The progress inside is not even , To the rear , The more time a cycle takes .

The code is as follows :

import math
import time
f = open(" Prime number below 100 million .txt", "w")
time_start = time.time() # Record the start time 
num = [];
i = 2
M=100000000
h = 0
for i in range(2, M):
print(" Progress is :{:.2%}".format(i/ M))
j = 2
for j in range(2, math.ceil(math.sqrt(i))):
if (i % j == 0):
break
else:
num.append(i)
h=h+1
print(h)
if h%10==0:
f.write(str(i)+"\n")
else:
f.write(str(i)+"\t")
time_end = time.time() # Record the end time 
time_sum = time_end - time_start # The calculated time difference is the execution time of the program , The unit is in seconds /s
# Printout 
print("\n")
print(" This calculation is less than %d All prime numbers of "%M)
print(" It took time :%.2f s"%time_sum)
print(" After calculation , Altogether %d A prime number "%len(num))
# print(" Namely :")
# print(num)
f.close()

This is the end result :

This is all the primes calculated :


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