Open In App

Sum of all Primes in a given range using Sieve of Eratosthenes

Last Updated : 06 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a range [l, r], the task is to find the sum of all the prime numbers in the given range from l to r both inclusive.

Examples:  

Input : l = 10, r = 20
Output : 60
Explanation: Prime numbers between [10, 20] are: 11, 13, 17, 19
Therefore, sum = 11 + 13 + 17 + 19 = 60

Input : l = 15, r = 25
Output : 59
Explanation: Prime numbers in range [15, 25] are 17, 19, and 23.

We have discussed both Naive and Sieve based solutions of this problem in Sum of all the prime numbers in a given range



Next Article

Similar Reads