TechTorch

Location:HOME > Technology > content

Technology

Implementing the Sieve of Eratosthenes in C: A Step-by-Step Guide

June 29, 2025Technology1934
Implementing the Sieve of Eratosthenes in C: A Step-by-Step Guide The

Implementing the Sieve of Eratosthenes in C: A Step-by-Step Guide

The Sieve of Eratosthenes is one of the ancient algorithms used to find all prime numbers up to a given limit. It is particularly interesting due to its simplicity and efficiency in finding prime numbers. In this article, we will explore how to implement the sieve in C, both with a straightforward approach and delve into the intricacies of an actor-style implementation.

Standard Sieve of Eratosthenes Algorithm

The classical Sieve of Eratosthenes involves creating a boolean array and marking the multiples of each prime starting from 2. Here is a C implementation of the sieve:

#include 
using namespace std;
void SieveOfEratosthenes(int n)
{
    // Create a boolean array
    // all entries it as true. A value in prime[i] will
    // finally be false if i is Not a prime else true.
    bool prime[n   1];
    memset(prime, true, sizeof(prime));
    for (int p  2; p * p  n; p  )
    {
        // If prime[p] is not changed then it is a prime
        if (prime[p]  true)
        {
            // Update all multiples of p
            for (int i  p * 2; i  n; i   p)
                prime[i]  false;
        }
    }
    // Print all prime numbers
    for (int p  2; p  n; p  )
    {
        if (prime[p])
            cout  p  ' ';
    }
}
// Driver Program to test above function
int main()
{
    int n  30;
    cout  "Prime numbers up to 30 are: "  endl;
    SieveOfEratosthenes(n);
    return 0;
}

This program will output all prime numbers up to 30. The algorithm works by initializing a boolean array and marking non-prime numbers. Finally, it prints out the prime numbers.

Actor-Style Implementation in C

Implementing the Sieve of Eratosthenes in an actor-style approach in C involves simulating the behavior of actors using threads or lightweight processes. In this implementation, each actor will filter out non-multiples of a given prime and pass the remaining numbers to the next actor.

#include 
#include 
#include 
#include 
int main()
{
    // Implementing line:1 from the pseudo-code
    int n;
    printf("Enter the limit: ");
    scanf("%d", n);
    int A[n];
    // Implementing line:3 from the pseudo-code
    for (int i  2; i  n; i  )
        A[i]  1;
    int i, j; // i is indexing variable and j is multiplier
    // Implementing lines 4 to 6 from the pseudo-code
    for (i  2; i  n; i  )
    {
        if (!A[i])
            continue;
        if (i % 2  0 || i % 3  0 || i % 5  0)
            A[i]  0;
        for (j  2; j * i  n; j  )
        {
            A[i * j]  0;
        }
    }
    // Implementing line 7 from the pseudo-code
    for (i  2; i  n; i  )
    {
        if (A[i])
            printf("%d ", i);
    }
    getch();
    return 0;
}

In this code, we initialize the array and mark non-prime numbers. Each actor checks if a number is not divisible by 2, 3, or 5. If a number is prime, it will continue the processing by filtering out its multiples.

actor-style Sieve of Eratosthenes in Bash

Even though the Sieve of Eratosthenes is traditionally implemented in C, it can also be simulated in a bash script in an actor-style manner. Here's how:

```bash #!/bin/bash USAGE: sieve limit # The first input is the base prime read P if [ -n "$P" ]; then echo "$P 3" # Have we gone over the limit if [ "$P" -gt 1 ]; then # No need to check any further... cat This script reads the limit and initializes the base prime. It then checks if the number is divisible by the current prime. If not, it passes the number to the next sieve.

Conclusion

The Sieve of Eratosthenes can be implemented in various ways, including in C and in an actor-style approach. The traditional C implementation is straightforward and efficient. The actor-style approach, however, reduces the problem into smaller tasks, making it easy to parallelize. Whichever method you choose, the key is to understand the algorithm and its efficiency.