In Python please:
Assume the availability of a function is_prime. Assume a variable n has been associated with positive integer. Write the statements needed to compute the sum of the first n prime numbers. The sum should be associated with the variable total.
Note: is_prime takes an integer as a parameter and returns True if and only if that integer is prime.
Please fix this wrong code.
---------------------------------
n >0
total =0
while total <= n:
if is_prime(n):
total = total+ n
if total <= n:
total = n
n = n + 1

Respuesta :

Answer:

n = int(input())

total = 0

for i in range(0, n):

   if is_prime(n):

       total += n

print("Total of first", n, "primes is", total)

Explanation:

Define n as an input variable, simply iterate through all numbers from 0 to n, adding them to a running count if the number is a prime as defined.

If you need code for checking if a number is prime - then please let me know.

Hope this helps!

ACCESS MORE