Given a number n, print all primes smaller than or equal to n.
Input: n = 10 Output: 2, 3, 5, 7 Input: n = 20 Output: 2, 3, 5, 7, 11, 13, 17, 19
We have discussed Sieve of Eratosthenes algorithm for the above task.
Below is Sieve of Sundaram algorithm.
printPrimes(n) [Prints all prime numbers smaller than n] 1) In general Sieve of Sundaram, produces primes smaller than (2*x + 2) for a number given number x. Since we want primes smaller than n, we reduce n-1 to half. We call it nNew. nNew = (n-1)/2; For example, if n = 102, then nNew = 50. if n = 103, then nNew = 51 2) Create an array marked[n] that is going to be used to separate numbers of the form i+j+2ij from others where 1 <= i <= j 3) Initialize all entries of marked as false. 4) // Mark all numbers of the form i + j + 2ij as true // where 1 <= i <= j Loop for i=1 to nNew a) j = i; b) Loop While (i + j + 2*i*j) 2, then print 2 as first prime. 6) Remaining primes are of the form 2i + 1 where i is index of NOT marked numbers. So print 2i + 1 for all i such that marked[i] is false.
Below is the implementation of above algorithm :
2 3 5 7 11 13 17 19
All red entries in below illustration are marked entries. For every remaining (or black) entry x, the number 2x+1 is prime.
Mark all the numbers which can be represented as i + j + 2ij
How does this work?
When we produce our final output, we produce all integers of the form 2x+1 (i.e., they are odd) except 2 which is handled separately.
Let q be an integer of the form 2x + 1. q is excluded if and only if x is of the form i + j + 2ij. That means, q = 2(i + j + 2ij) + 1 = (2i + 1)(2j + 1) So, an odd integer is excluded from the final list if and only if it has a factorization of the form (2i + 1)(2j + 1) which is to say, if it has a non-trivial odd factor. Source: Wiki
This article is contributed by Anuj Rathore. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Segmented Sieve (Print Primes in a Range)
- Sum of all Primes in a given range using Sieve of Eratosthenes
- Print all multiplicative primes <= N
- Print all safe primes below N
- Print all Proth primes up to N
- Print all Jumping Numbers smaller than or equal to a given value
- Length of largest sub-array having primes strictly greater than non-primes
- Count primes that can be expressed as sum of two consecutive primes and 1
- Count of primes below N which can be expressed as the sum of two primes
- Segmented Sieve
- Sieve of Atkin
- Sieve of Eratosthenes
- Bitwise Sieve
- Sieve of Eratosthenes in 0(n) time complexity
- How is the time complexity of Sieve of Eratosthenes is n*log(log(n))?
- Number of unmarked integers in a special sieve
- Prime Factorization using Sieve O(log n) for multiple queries
- Longest sub-array of Prime Numbers using Segmented Sieve
- Non-repeating Primes