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 email@example.com. 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
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Find the Product of first N Prime Numbers
- Find count of Almost Prime numbers from 1 to N
- Find product of prime numbers between 1 to n
- Python program to print all Prime numbers in an Interval
- Find two distinct prime numbers with given product
- 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
- 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
- Quick ways to check for Prime and find next Prime in Java