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 :