How to Find the Sum of Prime Numbers in a Range in Python?[4 Examples]

In this Python tutorial, I will explain how to find the sum of prime numbers in a range in Python with different methods and relatable examples.

In Python, you can use the below two methods to get the sum of prime numbers in a range.

  • Using loops
  • With a custom function

Find the Sum of Prime Numbers in a Range in Python

A number that is only divisible by 1 or itself is called a prime number. For example: 2, 3, 5, 7, 11, etc.

Here, we will see how to sum these prime numbers in a range, meaning we will sum all prime numbers present between, let us say, 1 to 50.

I will write a program in Python that will find all the prime numbers presented between 1 and 50, and then it will sum up all these numbers and show the output.

Here, we will ask the user to enter the range so that the user will enter the lower range and upper range as per their requirement.

Here is the complete code to get the sum of prime numbers in a range in Python using loops with different conditional statements:

def is_prime_numbers(n): 
    for i in range(2,n//2+1): 
        if(n%i==0): 
            return(0) 
    return(1) 
 
lower_range = int(input("Enter your lower range:")) 
upper_range = int(input("Enter your upper range:")) 
prime_numbers_list = []
sum_of_prime_numbers = 0 
for i in range(lower_range, upper_range+1): 
    if(is_prime_numbers(i)): 
        prime_numbers_list.append(i) 
print('The sum of prime numbers:', sum(prime_numbers_list))

Here, we are asking the user to select the range by using the code below:

lower_range = int(input("Enter your lower range:")) 
upper_range = int(input("Enter your upper range:")) 

When the user selects the number range, I use a for loop in Python to take each number and check if that is a prime number or not.

I wrote a Python function that will check if a number is prime or not.

def check_for_prime_numbers(n): 
    for i in range(2,n//2+1): 
        if(n%i==0): 
            return(0) 
    return(1) 

If the number is a prime number, then I add that number to an empty Python list, and finally, I use the sum() function in Python to get the sum of all these prime numbers.

You can see below in the screenshot after I executed the code, it gave the output:

Sum of the prime numbers in a range in python
Enter your lower range: 7
Enter your upper range: 27
The sum of prime number: 90

If you do not want to use the sum() function in Python, you can also use the += addition assignment operator in Python like the below source code:

def check_for_prime_numbers(n): 
    for i in range(2, n): 
        if(n%i==0): 
            return(0) 
    return(1) 
 
lower_range_number = int(input("Enter your lower range: ")) 
upper_range_number = int(input("Enter your upper range: ")) 
list_of_prime_numbers = []
sum_of_prime_numbers_in_range = 0 
for i in range(lower_range_number, upper_range_number+1): 
    if(check_for_prime_numbers(i)): 
        list_of_prime_numbers.append(i) 
for i in list_of_prime_numbers:
    sum_of_prime_numbers_in_range += i 
print('The sum of prime number:',sum_of_prime_numbers_in_range)

Refers to the below-presented image to see the output after the code runs successfully.

How to find the sum of prime numbers in a given range in Python

In the is_prime function in Python, I have used the expression number**0.5 to calculate the square root of the number. The square root is involved in the prime number for checking the logic.

When checking for the factors to determine if a number is prime, we only need to check up to the square root of the number. In Python, it is the more accurate and faster way to check for the prime number.

Here is the full code to find the sum of prime numbers in a given range in Python.

def is_prime(number):
    if number < 2:
        return False
    for i in range(2, int(number**0.5) + 1):
        if number % i == 0:
            return False
    return True
 
def sum_of_prime_numbers(start, end):
    current_number = start
    prime_sum = 0
    while current_number <= end:
        if is_prime(current_number):
            prime_sum += current_number
        current_number += 1
    return prime_sum
 
start_range = int(input("Enter the number to start the range: "))
end_range = int(input("Enter the number to end of the range: "))
 
result = sum_of_prime_numbers(start_range, end_range)
print(f"The sum of prime numbers in the range {start_range} to {end_range} is: {result}")

Output:

Enter the number to start the range: 3
Enter the number to end of the range: 17
The sum of prime numbers in the range 3 to 17 is: 56

Refer to the image below it will display the output after executing the code.

Find Prime Numbers in a Range in Python

Here, I have created a custom function in Python that will take two arguments i.e. lower range and upper range to find the sum of prime numbers in a range in Python.

Refers to the below full code to find the sum of the prime numbers in a given range in Python.

def check_prime_numbers(lower_range, upper_range):
    sum_of_prime_numbers = 0
 
    for num in range(lower_range, upper_range + 1):
        if num > 1:
            for i in range(2, int(num ** 0.5) + 1):
                if (num % i) == 0:
                    break
            else:
                sum_of_prime_numbers += num
 
    return sum_of_prime_numbers
 
result = check_prime_numbers(3, 11)
print(f"The sum of prime numbers in the given interval is: {result}")

Output:

The sum of prime numbers in the given interval is: 26

The below screenshot shows the output after the code gets executed.

sum of all prime numbers in a given range in python

Conclusion

I hope this article helps you to understand the procedure to find the sum of prime numbers in a given range in Python. Here, I have demonstrated this with different examples to find the sum of prime numbers in a given range such as using loops and custom functions.

You may also like to read: