Sieve() function

hello all, I was wondering if i can get some assistance with the Sieve algorithm. I have to make a bit array class that prints a array of bits in binary. But what sieve does is that it will show the prime numbers that are within the bit array size. If anyone could give me any idea that would be great. The function is supposed to be in a header file and is called into my main function here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>

using namespace std;

#include "Header.h"
#include "Seive .h"

int main()
{
    unsigned int i, max, counter = 0;
    
    cout << "\nEnter a positive integer for the maximum value: ";
    cin >> max;
    
    BitArray ba(max);
    
    Seive(ba);                    // find the primes (marking the bits)

    
    cout << "The bit array looks like this: \n"
    << ba
    << '\n';
    
    
    cout << "\nPrimes less than " << max << ':' << '\n';
    for (i = 0; i < max; i++)
    {
        if (ba.Query(i))
        {
            counter++;
            cout << i;
            if (counter % 8 == 0)
            {
                cout << '\n';
                counter = 0;
            }
            else
                cout << '\t';
        }
    }
    
    
    cout << "\nGoodbye!\n";
    return 0;
}

Last edited on
closed account (48T7M4Gy)
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
Topic archived. No new replies allowed.