Given a range [l, r], the task is to find the sum of all the prime numbers within that range.
Input : l=1 and r=6 Output : 10 Input : l=4 and r=13 Output : 36
Approach 1: (Naive Approach)
Iterate the loop from ‘l’ to ‘r’ and add all the numbers which are prime.
Below is the implementation of the above approach:
Approach 2: (Dynamic Programming)
- Declare an array dp and arr
- Fill the array arr to 0
- Iterate the loop till sqrt(N) and if
arr[i] = 0(marked as prime), then set all of it’s multiples as non-prime by marking the respective location as 1
- Update the dp array with the running prime numbers sum, where each location ‘dp[i]’ holds the sum of all the prime numbers withing the range [1, i]
# Python3 Program to computer sum of prime number
# in a given range
import math # for sqrt and ceil function
# Suppose the constraint is N<=1000 N = 1000 # Declare an array for dynamic approach dp =  * (N + 1) # Method to compute the array def seive(): # Declare an extra array as array array =  * (N + 1) array = 1 array = 1 # Iterate the loop till sqrt(N) # Time Complexity is O(log(n) X sqrt(N)) for i in range(2, math.ceil(math.sqrt(N) + 1)): # if ith element of arr is 0 # i.e. marked as prime if array[i] == 0: # mark all of it's multiples till N as # non-prime by setting the locations to 1 for j in range(i * i, N + 1, i): array[j] = 1 runningPrimeSum = 0 # Update the array 'dp' with the running sum # of prime numbers within the range [1, N] # Time Complexity is O(n) for i in range(1, N + 1): if array[i] == 0: runningPrimeSum += i # Here, dp[i] is the sum of all the prime numbers # within the range [1, i] dp[i] = runningPrimeSum # Driver Code l = 4 r = 13 seive() print(dp[r] - dp[l - 1]) # This code is contributed by Vivek Kumar Singh [tabby title="C#"]
- Prime numbers in a given range using STL | Set 2
- Sum of all prime divisors of all the numbers in range L-R
- Print prime numbers in a given range using C++ STL
- Numbers in range [L, R] such that the count of their divisors is both even and prime
- Count numbers from range whose prime factors are only 2 and 3
- K-Primes (Numbers with k prime factors) in a range
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Find the highest occurring digit in prime numbers in a range
- Queries for the difference between the count of composite and prime numbers in a given range
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Print the nearest prime number formed by adding prime numbers to N
- Print prime numbers with prime sum of digits in an array
- Check if a prime number can be expressed as sum of two Prime Numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.