w3resource

Python: Compute the sum of first n given prime numbers

Python Basic - 1: Exercise-52 with Solution

Sum of First n Primes

Write a Python program to compute the sum of the first n prime numbers.

Input:
n ( n ≤ 10000). Input 0 to exit the program.
Input a number (n≤10000) to compute the sum:(0 to exit)
25
Sum of first 25 prime numbers:
1060

Visual Presentation:

Python: Compute the sum of first n given prime numbers

Sample Solution:

Python Code:

# Set the maximum limit for prime number computation
MAX = 105000

# Print statement to prompt the user to input a number (n≤10000) to compute the sum (0 to exit)
print("Input a number (n≤10000) to compute the sum:(0 to exit)")

# Boolean list to track prime numbers using the Sieve of Eratosthenes algorithm
is_prime = [True for _ in range(MAX)]
is_prime[0] = is_prime[1] = False

# Iterate through the range to mark non-prime numbers
for i in range(2, int(MAX ** (1 / 2)) + 1):
  if is_prime[i]:
    for j in range(i ** 2, MAX, i):
      is_prime[j] = False 

# Create a list of prime numbers within the given range
primes = [i for i in range(MAX) if is_prime[i]] 

# Infinite loop to continuously accept user input until 0 is entered
while True:
  # Prompt the user to input a number
  n = int(input())
  
  # Check if the entered number is 0, and exit the loop if true
  if not n:
    break
  
  # Print statement to display the sum of the first n prime numbers
  print("Sum of first", n, "prime numbers:")
  
  # Calculate and print the sum of the first n prime numbers using list slicing
  print(sum(primes[:n]))

Sample Output:

Input a number (n≤10000) to compute the sum:(0 to exit)
 25
Sum of first 25 prime numbers:
1060

Explanation:

Here is a breakdown of the above Python code:

  • First the code sets the maximum limit for prime number computation (MAX).
  • It initializes a boolean list (is_prime) to track prime numbers using the Sieve of Eratosthenes algorithm.
  • The code creates a list of prime numbers (primes) within the specified range.
  • It enters an infinite loop to continuously accept user input until 0 is entered.
  • Inside the loop, it prompts the user to input a number (n) and calculates the sum of the first n prime numbers.
  • The loop exits if the entered number is 0.

Flowchart:

Flowchart: Python - Compute the sum of first n given prime numbers

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to find the difference between the largest integer and the smallest integer which are created by 8 numbers from 0 to 9. The number that can be rearranged shall start with 0 as in 00135668.
Next: Write a Python program that accept a even number (>=4, Goldbach number) from the user and create a combinations that express the given number as a sum of two prime numbers. Print the number of combinations.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-exercises/basic/python-basic-1-exercise-52.php