C/C++ Boolean array to identify prime numbers - C++

AI Thread Summary
The algorithm in question is an implementation of the Sieve of Eratosthenes, a classic method for identifying prime numbers. It begins by initializing a boolean array, `table`, with `N` elements, all set to true, indicating that all numbers are initially presumed prime. The first element is then set to false, as 0 is not prime. The algorithm iterates through the array starting from 2, the first prime number. For each prime found, it marks all of its multiples as not prime by setting their corresponding values in the `table` to false. This process continues until the algorithm has processed all numbers up to `N`. The variable `count` is initialized but not utilized in this specific implementation, which may lead to confusion regarding its purpose. Overall, the algorithm effectively filters out non-prime numbers, leaving only primes in the boolean array.
sandy.bridge
Messages
797
Reaction score
1
Hey guys, just looking for an explanation for the following algorithm. It is in my textbook, and there isn't really an explanation. I don't really see how the algorithm works, but I will add what I do know, and hopefully one of you can help. Thanks.

PHP:
//this initial declarations produces an array with N elements.
int N = 40;
bool table[N];
int count = 0;

//this while loop assigns every element in the array to true.
int i = 0;
while (i < N)
{
     table[i]=true;
     i=i+1;
}

//from here is where I get lost.
table[0]=false; //assigns false value to the first element in the array.
i=2; //starts the next loop at 2.
while (i < N)
{
     if (table[i]
     {
          cout << i << " is prime." << endl;
          int j = 2*i;

          while (j < N)
          { 
              table[j]=false;
               j=j+i;
          }
     }
i=i+1;
}

Furthermore, I am not entirely sure why count was initialized at the beginning of the algorithm, I never saw it used.
 
Technology news on Phys.org
sandy.bridge said:
Hey guys, just looking for an explanation for the following algorithm. It is in my textbook, and there isn't really an explanation. I don't really see how the algorithm works, but I will add what I do know, and hopefully one of you can help. Thanks.

PHP:
//this initial declarations produces an array with N elements.
int N = 40;
bool table[N];
int count = 0;

//this while loop assigns every element in the array to true.
int i = 0;
while (i < N)
{
     table[i]=true;
     i=i+1;
}

//from here is where I get lost.
table[0]=false; //assigns false value to the first element in the array.
i=2; //starts the next loop at 2.
while (i < N)
{
     if (table[i]
     {
          cout << i << " is prime." << endl;
          int j = 2*i;

          while (j < N)
          { 
              table[j]=false;
               j=j+i;
          }
     }
i=i+1;
}

Furthermore, I am not entirely sure why count was initialized at the beginning of the algorithm, I never saw it used.

Its basically an implementation of the sieve of eratosthenes method to find primes.
http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

The boolean array, table, keeps track of a boolean value for every number up to N that tells whether that number is prime or not. The code then starts from the first prime (2) and excludes all multiples of 2 by setting their boolean value to false. Then the next lowest true value is obviously the next prime, then it repeats this for this number and excludes all its multiples in the table. So then it keeps repeating this process until it reaches the end of the array.
 
Perfect, thanks!
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top