First of all, I think you can split your code by defining a function. However, there is a drawback of using a regular function in this case because every time a normal function return
a value, the next call to the function will execute the complete code inside the function again. Since you are iterating 2 million times, it would be better to:
- Have a function that gives you the next prime number and provisionally returns the control to the caller. Such functions are known as GENERATORS.
- To define a generator function just use the
yield
command instead of return
.
- When you use generators , it is like knowing that the function will be called again and when it happens the execution inside the function continues right after the
yield
instruction instead of going over the whole function again.
- The advantage of this approach is that on the long run of an iterator you avoid the consumption all of the system's memory.
I recommend you to have a look at this article about generators in python. It provides a more extensive explanation for this example.
The solution would be something like this:
import math
# Check if a number is prime
def is_prime(number):
if number > 1:
if number == 2:
return True
if number % 2 == 0:
return False
for current in range(3, int(math.sqrt(number) + 1), 2):
if number % current == 0:
return False
return True
return False
# Get the next after a given number
def get_primes(number):
while True:
if is_prime(number):
yield number
# Next call to the function will continue here!
number += 1
# Get the sum of all prime numbers under a number
def sum_primes_under(limit):
total = 2
for next_prime in get_primes(3):
if next_prime < limit:
total += next_prime
else:
print(total)
return
# Call the function
sum_primes_under(2000000)