Boolean array to identify prime numbers - C++

Click For Summary

Discussion Overview

The discussion revolves around an algorithm for identifying prime numbers using a boolean array, specifically an implementation of the Sieve of Eratosthenes method in C++. Participants seek clarification on the algorithm's functionality and specific code elements.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant expresses confusion about the algorithm's operation and the purpose of the variable 'count', noting it is initialized but not used.
  • Another participant identifies the algorithm as an implementation of the Sieve of Eratosthenes, explaining that the boolean array tracks whether numbers up to N are prime.
  • The explanation includes that the algorithm starts with the first prime (2) and marks all multiples of each prime as non-prime.
  • There is a reference to a Wikipedia article for further reading on the Sieve of Eratosthenes.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the clarity of the algorithm, as one participant is still confused about certain aspects, while another provides an explanation. The discussion remains partially unresolved regarding the purpose of the 'count' variable.

Contextual Notes

The discussion does not address potential limitations of the algorithm or assumptions about the input values. There is also no exploration of the implications of the boolean array's size or initialization.

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]
     {
          count << 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]
     {
          count << 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!
 

Similar threads

  • · Replies 36 ·
2
Replies
36
Views
1K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 28 ·
Replies
28
Views
5K
  • · Replies 15 ·
Replies
15
Views
4K
Replies
20
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 97 ·
4
Replies
97
Views
9K
Replies
14
Views
3K
Replies
1
Views
2K
Replies
12
Views
2K