Reputation: 113
I'm running the following code to find the sum of the first 10,000,000 prime numbers. How can I optimize it such that it doesn't take forever to obtain the result(the sum of prime numbers)?
sum=0
num=2
iterator=0
while iterator<10000000:
prime = True
for i in range(2,num):
if (num%i==0):
prime = False
if prime:
sum=sum+num
# print (num, sum, iterator)
iterator=iterator+1
num=num+1
print(sum)
Upvotes: 1
Views: 631
Reputation: 22324
It turns out the Sieve of Eratosthenes is fairly simple to implement in Python by using correct slicing. So you can use it to recover the n first primes and sum them.
It was pointed out in Joran Beasley's answer that an upper bound for the n-th prime is n * ln(n) + n * ln( ln(n) )
, which we can use and then discrard the extra primes. Note that this bound does not work for n smaller than 6.
from math import log
def sum_n_primes(n):
# Calculate the upper bound
upper = int(n * ( log(n) + log(log(n))))
# Prepare our Sieve, for readability we make index match the number by adding 0 and 1
primes = [False] * 2 + [True] * (upper - 1)
# Remove non-primes
for x in range(2, int(upper**(1/2) + 1)):
if primes[x]:
primes[2*x::x] = [False] * (upper // x - 1) # Replace // by / in Python2
# Sum the n first primes
return sum([x for x, is_prime in enumerate(primes) if is_prime][:n])
It takes a few seconds, but outputs that the sum of the 10,000,000 first primes is 870530414842019
.
If you need n to be any higher, one solution would be to improve your upper bound.
Upvotes: 0
Reputation: 114038
the 10,000,000 th prime is approximately n * ln(n) + n * ln( ln(n) )
or ~188980383
... then you can use a sieve to find all primes under that value (discard any extras ... (ie you will get about 50k extra prime numbers when using 10million, note this took approximately 8 seconds for me))
see also : Finding first n primes?
see also : Fastest way to list all primes below N
Upvotes: 3
Reputation: 168
You can use the Sieve of Eratosthenes. It's a much faster method to find the first n prime numbers.
Upvotes: 1