Write a program to find sum of all prime numbers between 1 to n.
Input : 10 Output : 17 Explanation : Primes between 1 to 10 : 2, 3, 5, 7. Input : 11 Output : 28 Explanation : Primes between 1 to 11 : 2, 3, 5, 7, 11.
A simple solution is to traverse all numbers from 1 to n. For every number, check if it is a prime. If yes, add it to result.
An efficient solution is to use Sieve of Eratosthenes to find all prime numbers from till n and then do their sum.
This article is contributed by Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Program to find Prime Numbers Between given Interval
- Find two prime numbers with given sum
- Python program to print all Prime numbers in an Interval
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Find the Product of first N Prime Numbers
- Find product of prime numbers between 1 to n
- Find count of Almost Prime numbers from 1 to N
- Find two distinct prime numbers with given product
- Find a sequence of N prime numbers whose sum is a composite number
- Find the prime numbers which can written as sum of most consecutive primes
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Find the highest occurring digit in prime numbers in a range
- Program to find the Nth Prime Number
- Print the nearest prime number formed by adding prime numbers to N
- Print prime numbers with prime sum of digits in an array