题目:https://projecteuler.net/problem=10
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
MATLAB代码
>> sum(primes(2e6))
ans =
142913828922
Python代码
import math
primes = [2]
t = 1
while t <= 2e6:
t += 2
isprime = True
for i in primes:
if t%i == 0:
isprime = False
break
if i > math.sqrt(t):
isprime = True
break
if not(isprime):
continue
#print(t)
primes.append(t)
print(sum(primes))
Pyhton运行时间稍长