I have written the following block of code to calculate the sum of all the primes below a certain number-2 000 000 in this case to be precise, however it takes quite some time to execute; 20 second:
def summation_of_primes_below_n(n):
list = []
sum = 0
for i in range(2, n):
if checks_if_prime(i) == True:
list.append(i)
return list
for j in list:
sum = sum + j
return sum
def checks_if_prime(n):
if n == 2:
return True
import math
for i in range(2, math.ceil(math.sqrt(n))+1):
if n%i == 0:
return False
elif i == math.ceil(math.sqrt(n)):
return True
print(summation_of_primes_below_n(2000000))
So I was wondering if there was a way to make my code more efficient. I would greatly appreciate suitable advice on the same. Also, I would prefer that you give more basic solutions since I am a beginner and provide the logic for the same. Thanks a lot!