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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Find all numbers between range L to R such that sum of digit and sum of square of digit is prime
- Prime numbers after prime P with sum S
- Check if a prime number can be expressed as sum of two Prime Numbers
- Print prime numbers with prime sum of digits in an array
- Sum of prime numbers without odd prime digits
- Count all prime numbers in a given range whose sum of digits is also prime
- Bitwise AND of the sum of prime numbers and the sum of composite numbers in an array
- Program to find Prime Numbers Between given Interval
- C/C++ Program to find Prime Numbers between given range
- Print the nearest prime number formed by adding prime numbers to N
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Check if a number is Prime, Semi-Prime or Composite for very large numbers
- Permutation of first N positive integers such that prime numbers are at prime indices
- Permutation of first N positive integers such that prime numbers are at prime indices | Set 2
- Sum of each element raised to (prime-1) % prime