Generate Prime Numbers Using Sieve of Sundaram in C++



The Sieve of Sundaram method is used to generate the prime number within the given range. In this method, first we mark the indices with prime number using the mathematical formula. Then, we use the unmarked indices to get the prime numbers within the given range.

In this article, we have defined a range i.e. 'm = 30'. Our task is to generate prime numbers up to 'm' using the Sieve of Sundaram method in C++.

Example

Here is an example of generating prime numbers up to 10 using the Sieve of Sundaram method:

Input:
M = 10

Output:
2 3 5 7

The explanation of the above example is given below:

  • Marking each index of N as false:

    M = 10, N = (M - 2) / 2 = 4
    
    Index[i] = 1, 2, 3, 4
    marked[i] = false, false, false, false
    
  • Marking non-prime numbers as true using the formula i+j+2ij <= N:

    For i = 1, j = 1:
        1 + 1 + 2*1*1 = 4, marked[4] = true
        j = 2:
        1 + 2 + 2*1*2 = 7; 7 > 4
    For i = 2, 
        j = 2:
        2 + 2 + 2*2*2 = 10; 10 > 4
    For i = 3, 
        j = 3:
        3 + 3 + 2*3*3 = 21; 21 > 4
    For i = 4, 
        j = 4:
        4 + 4 + 2*4*4 = 40; 40 > 4
    New marked array: 1(F), 2(F), 3(F), 4(T)
    
  • Use prime = 2i + 1 to find prime numbers with i marked as false:

    For i = 1, Prime = 2*1 + 1 = 3; For i = 2, Prime = 2*2 + 1 = 5
    For i = 3, Prime = 2*3 + 1 = 7
    
    Output:
    2 3 5 7
    

Steps to Implement Sieve of Sundaram Method

The following steps are used to implement the Sieve of Sundaram method for generating prime numbers:

  • First, we calculate the value of N using the formula (m - 2) / 2.
  • Then we defined a boolean array and marked each element as false i.e. as prime.
  • Then we used a nested for loop that marks the elements as true that are composite numbers. The inner loop uses the formula (i + j + 2 * i * j) <= N to mark the index as true.
  • The indexes which are still marked as false, are then used to find the prime numbers using the formula 2 * i + 1
  • The above step generates the prime numbers starting from 3, so we push '2' as prime number manually. Then, the result is printed with all prime numbers.

C++ Program to Implement Sieve of Sundaram

The following code implements the above steps to implement the Sieve of Sundaram method to generate prime numbers in C++.

#include <bits/stdc++.h>
using namespace std;

void sieveOfSundaram(int m) {
    int N = (m - 2) / 2;

    bool marked[N + 1];
    memset(marked, false, sizeof(marked));

    for (int i = 1; i <= N; i++) {
        for (int j = i; (i + j + 2 * i * j) <= N; j++) {
            marked[i + j + 2 * i * j] = true;
        }
    }

    if (m > 2)
        cout << 2 << " ";

    for (int i = 1; i <= N; i++) {
        if (!marked[i])
            cout << 2 * i + 1 << " ";
    }
}

int main() {
    int m = 30;
    cout << "Prime numbers up to " << m << " are:\n";
    sieveOfSundaram(m);
    return 0;
}

The output of the above code is:

Prime numbers up to 30 are:
2 3 5 7 11 13 17 19 23 29 
Updated on: 2025-05-06T19:00:51+05:30

499 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements