Implement Wheel Sieve to Generate Prime Numbers in C++



The wheel Sieve method is used to find the prime numbers within a given range. Wheel factorization is a graphical method for manually performing a preliminary to the Sieve of Eratosthenes that separates prime numbers from composites.

In this method, prime numbers in the innermost circle have their multiples in similar positions as themselves in the other circles, forming spokes of primes and their multiples. Multiple of these prime numbers in the innermost circle form spokes of composite numbers in the outer circles.

In this article, we have set the limit to 100. Our task is to implement the wheel sieve method to find the prime number up to the given limit in C++.

Example

Here is an example of wheel sieve method to find the prime numbers:

Input:
Limit = 30

Output:
Prime numbers up to 100 are:
2 3 5 7 11 13 17 19 23 29

Steps to Implement Wheel Sieve Method

The following steps implements the wheel sieve method to find the prime numbers.

  • We have defined the wheelSieve() function to implement the wheel sieve method. We have passed limit as the argument that specifies the limit up to which we want to find the prime numbers.
  • We have set the three initial prime numbers i.e. 2, 3, 5. Using these we have defined the wheel size i.e. lcm(2, 3, 5).
  • Using the lcm of the basic primes, we have defined the pattern array that stores all the elements that are co-prime to 30.
  • We have declared an array isPrime that stores whether the element is prime or not. Initially, all the elements in this array are marked as false i.e. non-prime, and later on mark only prime numbers as true.
  • Then, we use the wheel pattern to generate the candidates for prime numbers using num = base * wheelSize + pattern[i];. The candidates that are co-prime to 30 are initially marked as true.
  • Then, we check if there are still remaining primes using the prime numbers that are already marked as true. It start with 7 as it is the first element marked as true.
  • If any element is found to be multiple of these elements i.e. already prime numbers, we mark them as false.
  • Then we return the elements that are marked as true or which are not marked as false. These are the required prime numbers.

C++ program to Implement Wheel Sieve Method

The following code implements the above steps to find the prime numbers using the wheel sieve method.

#include <iostream>
using namespace std;

#define MAX_LIMIT 100  

void wheelSieve(int limit) {
    // Defining wheel size and pattern
    const int wheelSize = 30;
    const int pattern[] = {1, 7, 11, 13, 17, 19, 23, 29};  // Co-prime numbers to 30
    const int numSpokes = sizeof(pattern) / sizeof(pattern[0]);  // Number of elements in the pattern

    // Store whether a number is prime, initially all set to false
    bool isPrime[MAX_LIMIT + 1] = {false}; 
    // Base primes to manually set as prime (2, 3, 5)
    isPrime[2] = true;
    isPrime[3] = true;
    isPrime[5] = true;

    // Mark candidates using the wheel pattern
    for (int base = 0; base <= limit / wheelSize; ++base) {
        for (int i = 0; i < numSpokes; ++i) {
            int num = base * wheelSize + pattern[i];
            if (num <= limit) {
                isPrime[num] = true;
            }
        }
    }

    // Sieve for prime multiples of numbers starting from 7
    for (int i = 7; i * i <= limit; ++i) {
        if (isPrime[i]) {
            for (int j = i * i; j <= limit; j += i) {
                isPrime[j] = false;  // Marking multiples as non-prime
            }
        }
    }

  
    cout << "Prime numbers up to " << limit << " are:" << endl;
    for (int i = 2; i <= limit; ++i) {
        if (isPrime[i]) {
            cout << i << " ";  
    }
}
    cout << endl;
}


int main() {
    int limit = 100;  
    wheelSieve(limit);  
    return 0;
}

The output of the above code is:

Prime numbers up to 100 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

Complexity of Wheel Sieve Method

  • Time Complexity: The time complexity of the Wheel Sieve method to generate prime numbers is O(n log(logn)).
  • Space Complexity: The space complexity of the Wheel Sieve method to generate prime numbers is O(n).
Updated on: 2025-05-05T18:42:57+05:30

388 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements