You can download this code by clicking the button below.
This code is now available for download.
This function calculates and returns the sum of the first n prime numbers.
Technology Stack : Built-in functions (range, int, **0.5, %, ==, while, len, +)
Code Type : Mathematical calculation function
Code Difficulty : Intermediate
def sum_n_primes(n):
def is_prime(num):
if num <= 1:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
def get_primes():
primes = []
num = 2
while len(primes) < n:
if is_prime(num):
primes.append(num)
num += 1
return primes
primes = get_primes()
return sum(primes)