这是我用Python编写的计算小于给定数字的所有质数之和的代码。
我还能做哪些优化呢?
import math
primes = [2,] #primes store the prime numbers
for i in xrange(3,20000,2): #i is the test number
x = math.sqrt(i)
isprime = True
for j in primes: #j is the devider. only primes are used as deviders
if j <= x:
if i%j == 0:
isprime = False
break
if isprime:
primes.append(i,)
print sum (primes,)
if isprime == True:
替换为if isprime:
。 - mike jones