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

In summary, the conversation discusses creating a program in C++ to calculate the number of prime numbers up to a specified number using bitshifting and an array of 32-bit integers. The individual is struggling with understanding how to use bitshifting and the code provided is their attempt at implementing it. They encounter a problem with the output and are seeking help to fix it.
  • #1
necromanzer52
12
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);

}

}
}



cout << 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
  • #2
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
 
  • #3
That changed absolutely nothing.
 
  • #4
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
 
  • #5
Ah. Thanks for that. I think I have it working now.
 

What is the sieve of Eratosthenes algorithm?

The sieve of Eratosthenes is a method for finding all prime numbers up to a given limit. It works by creating a list of numbers from 2 to the limit and repeatedly crossing out all multiples of each prime number, leaving behind only the primes.

How does the sieve of Eratosthenes work?

The algorithm involves marking all the numbers in a list initially as potential primes. Then, starting from the first prime number (2), all its multiples are marked as non-primes. This process is repeated for each prime number until all multiples have been marked. The remaining numbers in the list are the prime numbers.

What is the advantage of using bit arrays in the sieve of Eratosthenes algorithm?

Using bit arrays allows for a more efficient implementation of the algorithm, as it reduces the amount of memory needed. Instead of storing each number as an integer, bit arrays use a single bit to represent each number. This results in a smaller memory footprint and faster processing.

What is the time complexity of the sieve of Eratosthenes algorithm?

The time complexity of the algorithm is O(n log log n), where n is the limit up to which primes are being found. This makes it a very efficient algorithm for finding prime numbers.

How can the sieve of Eratosthenes be implemented in C++ using bit arrays?

To implement the sieve of Eratosthenes in C++ using bit arrays, you can first create a bit array of size n+1, where n is the limit up to which you want to find primes. Then, use a loop to iterate through the array and mark all numbers as potential primes. Next, use a nested loop to go through each prime number and mark its multiples as non-primes. Finally, the remaining numbers in the array will be the prime numbers, which can be printed or stored in a separate list for further use.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
756
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Programming and Computer Science
Replies
22
Views
774
  • Engineering and Comp Sci Homework Help
Replies
32
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
Replies
1
Views
950
Back
Top