Find primes using sieve of erasthothenes by way of bit arrays in C++

  • Context: Comp Sci 
  • Thread starter Thread starter necromanzer52
  • Start date Start date
  • Tags Tags
    Arrays Bit C++ Primes
Click For Summary

Discussion Overview

The discussion revolves around implementing the Sieve of Eratosthenes algorithm in C++ using bit arrays to find prime numbers up to a specified limit. Participants explore the technical challenges of using bit manipulation and bit shifting to represent prime numbers.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant shares their initial attempt at coding the Sieve of Eratosthenes using a bit array, expressing confusion over the implementation and output.
  • Another participant points out a potential issue with operator precedence in the condition used to check for primes, suggesting the use of parentheses to clarify the order of operations.
  • A further suggestion is made to modify the condition to check if the bit is set correctly, either by comparing to (1 << j) or checking for non-zero values.
  • The original poster indicates that the changes suggested did not resolve their issue initially.
  • Eventually, the original poster reports success in getting their code to work after applying the suggestions.

Areas of Agreement / Disagreement

The discussion shows a progression of troubleshooting steps, with some participants agreeing on the need for clarity in operator precedence, while the original poster's initial confusion indicates that the problem was not straightforward. The final success reported by the original poster suggests a resolution to their specific issue, but does not imply consensus on the broader topic of implementation.

Contextual Notes

Participants did not fully explore all aspects of the Sieve of Eratosthenes algorithm or the implications of using bit arrays, leaving some technical details and potential optimizations unaddressed.

necromanzer52
Messages
12
Reaction score
0
Hey. I have to create a program in C++ to calculate the number of prime numbers up to a specified number. I also have to print out the first and last 5, but I'll cross that bridge when I come to it.

I can do this fine with integers, but we have to do it where each slot in an array represents 32 bits. We have to use bitshifting to make each bit a 1 for a prime, and a 0 for a non-prime. It wasn't explained to us very well how we're supposed to do this, and I can't find any useful information on the internet. Here's what I have so far:

#include <iostream>
using namespace std;

#define N 100 // size of sieve used
#define size ((N+31)/32) //32 bits per integer hard coded
int sieve[size]; //declare array of bits

int main ()
{
int i, j, m, product, count;
count = 0;

for (i=0;i<size;i++)
{
sieve = 0xFFFFFFFF;
}
sieve [0] = 0xFFFFFFFC;

for (j=0;j<32;j++)
{
if (sieve[0] & (1 << j) == 1) //find the next prime
{
count++; //count the prime
product = 0;
for (m=2;product<32;m++) // set each multiple of the prime to 0.
{
product = m*j;

sieve[0] &~(1<<product);

}

}
}



count << count << endl;

return 0;
}

This is just my attempt to count the number of primes in the first 32 numbers, and it just keeps giving an output of 0.
I'm completely lost, and any help would be appreciated.
 
Physics news on Phys.org
C / C++ treat & as lower precendece than == (considered a flaw by some). Use parenthesis to force the order of operations:

Code:
		if ((sieve[0] & (1 << j)) == 1)     //find the next prime
 
That changed absolutely nothing.
 
It should be:

Code:
		if ((sieve[0] & (1 << j)) == (1 << j))     //find the next prime
or
Code:
		if ((sieve[0] & (1 << j)) != 0)     //find the next prime
 
Ah. Thanks for that. I think I have it working now.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 36 ·
2
Replies
36
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
5K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K