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
- Program to print first N Prime numbers
- Find two prime numbers with given sum
- Find the XOR of first N Prime Numbers
- 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 out the prime numbers in the form of A+nB or B+nA
- Find count of Almost Prime numbers from 1 to N
- Find product of prime numbers between 1 to n
- Find the Product of first N Prime Numbers
- Find the sum of prime numbers in the Kth array
- Find two distinct prime numbers with given product
- Find all the prime numbers of given number of digits
- Find the prime numbers which can written as sum of most consecutive primes
- Find a sequence of N prime numbers whose sum is a composite number