A Prime Number is a number that can only be divided by itself and 1 without remainders (e.g., 2, 3, 5, 7, 11). There are two ways to find the sum of the first n prime numbers between 1 and n using Python.
To solve this problem you need to know how to use the below concepts.
- for loop, range() function and while loop
- if-else condition
- Math module
Table of contents
1. Using loop
We can use for loop to calculate the sum of the first n prime numbers by iterating through numbers from 1 to n, checking if they are prime, and summing them.
Also, see:
- Python Programs to Check if Number is Prime Number
- Python Programs to Find Prime Numbers within a Range
Code Example: Calculate the sum of first 5 prime numbers.
Output:
2
3
5
7
11
The sum of the first 4 prime numbers is: 28
Explanation
- We are using a while loop to iterate over numbers starting from 2 until we get the expected number of prime numbers, which is 5 in this case.
- In each iteration of a loop, we are doing the following operation:
- Checking if the number is prime using
is_prime()function - Printing the number if its prime
- Summing it with
prime_sum - Incrementing the prime number counter (
prime_counter) by 1 - Increment the number by 1 to get the next number in the series, i.e., 3, 4, and so on…
- Iteration ends when
prime_counterreaches the expected prime number count.
- Checking if the number is prime using
is_prime(): It is a function to find the prime number, we check divisibilitynum % i == 0only up to the square root of the number, which significantly improves performance.- Edge case: Numbers less than or equal to 1 are immediately classified as not prime because prime numbers are greater than 1.
- Use of
math.sqrt: Themath.sqrtfunction calculates the square root ofn. - for Loop: The loop iterates from 2 up to and including √n, significantly reducing the number of checks needed compared to iterating through all numbers up to
n-1. - Modulo operator (
%): The modulo operator is used to check divisibility. Ifn % i == 0, theniis a divisor ofn, indicating that the number is not prime.
2. Using Python’s Built-in filter() and itertools
Steps to find the sum of first n prime numbers using filter() and itertools:
- Checking for Prime Numbers
The function
is_prime()checks if a number is prime as explained earlier.
To find the prime number, we check the divisibilitynum % i == 0only up to the square root of the number, which significantly improves performance. - Generating Numbers
itertools.count(2)creates an infinite sequence starting from2, increasing by1at each step. - Filter Prime Numbers
filter()appliesis_prime()to each number on the generated sequence.
Only keeps numbers for whichis_prime(num) == True(i.e., prime numbers).
Generates an infinite sequence of prime numbers. - Selecting the First N Prime Numbers
itertools.islice(primes, n)takes the firstnprime numbers from the infinite sequence. - Sum of prime numbers
sum()function adds all prime numbers together. - Printing the Result
The final sum of the first
nprime numbers is displayed.
Code Example
Output:
The sum of the first 4 prime numbers is: 17Code language: Python (python)
Leave a Reply