You can download this code by clicking the button below.
This code is now available for download.
Calculates the sum of the first n prime numbers
Technology Stack : Built-in functions (is_prime, range, int, **, +=, ==, while, if)
Code Type : Function
Code Difficulty : Intermediate
def sum_n_primes(n):
def is_prime(num):
if num < 2:
return False
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
return False
return True
def sum_of_primes():
total, count, num = 0, 0, 2
while count < n:
if is_prime(num):
total += num
count += 1
num += 1
return total
return sum_of_primes()