2

I want to use a bit-array and the Sieve of Eratosthenes algorithm to find all the primes within a certain range. My code compiles, but it prints all the non-prime numbers rather than prime numbers (except for 2 because I ask my Sieve function to print 2). Can someone review my code and give me some hints on how to fix it? Your help is greatly appreciated. Thank you!

Note: The requirement of my homework is to use a bit-array.

#include <stdio.h>
#include <stdlib.h>

//#define MAXBYTES 1000000
#define MAXBYTES 10


void setBit(unsigned int A[], int k);
unsigned getBit(unsigned int A[], int k);
void print_prime (int prime_num);
void sieve_Prime(unsigned int bit_arr[]);

int main (int argc, char** argv)
{
    //int bit_arr[MAXBYTES];      //This is the bit array (32 X MAXBYTES)
    unsigned int bit_arr[MAXBYTES];      //or bit_arr[MAXBYTES]
    int i;

    for (i=0; i < MAXBYTES; i++)
    {
        bit_arr[i] = 0x00;            //initialize all bits to 0s
    }

    setBit(bit_arr, 0);             //0 is not prime, set it to be 1
    setBit(bit_arr, 1);             //1 is not prime, set it to be 1

    sieve_Prime(bit_arr);
    printf("\n");

    return 0;

}

//Set the bit at the k-th position to 1
void setBit(unsigned int A[], int k)
{
    int i = k/32;
    int pos = k % 32;

    unsigned int flag = 1;      //flag = 0000 ..... 00001
    flag = flag << pos;         //flag = 0000...010...000 (shifted k positions)

    A[i] = A[i] | flag;         //Set the bit at the k-th position in A[i];
}

//get the bit at the k-th position
unsigned getBit(unsigned int A[], int k)
{
    int i =k/32;
    int pos = k % 32;

    unsigned int flag = 1;

    flag = flag << pos;

    if (A[i] & flag)
        return 1;
    else
        return 0;
}


void print_prime (int prime_num)
{
    //print a prime number in next of 8 columns
    static int numfound=0;

    if (numfound % 8 == 0)
        printf("\n");
    if (prime_num+1 < MAXBYTES*8)
        printf("%d\t", prime_num);
    numfound++;
}

void sieve_Prime(unsigned int bit_arr[])
{
    int i;
    int k;
    int next_prime = 2;

    print_prime(2);

    while (next_prime+1 < MAXBYTES*8)
    {
        k = next_prime;

        //multiples of next_prime is not primpe
        while(next_prime*k < MAXBYTES*8)
        {
            setBit(bit_arr, next_prime*k);      //set it to be 1
            k++;     
        }

        //find next_prime by skipping non-prime bits marked 1
        while (next_prime + 1 < MAXBYTES*8 && getBit(bit_arr, ++next_prime))
        {
            print_prime(next_prime);
        }
    }
}
4

2 回答 2

4

问题是你的循环:

while (next_prime + 1 < MAXBYTES*8 && getBit(bit_arr, ++next_prime))
{
    print_prime(next_prime);
}

您在设置位时继续打印东西(即当您知道它不是素数时)。所以基本上,你的循环是“打印我在寻找下一个素数时找到的所有数字”,而不是“在循环中查找下一个素数,然后打印下一个素数”。

我怀疑你想要类似的东西:

next_prime++; // We always want to at least move on once...
while (next_prime + 1 < MAXBYTES*8 && getBit(bit_arr, next_prime))
{
    next_prime++;
}
print_prime(next_prime);

我还没有检查代码是否有问题,但这肯定是首先要修复的问题

于 2013-08-17T18:02:22.777 回答
0

您可以简单地通过添加下一个初始化构造来初始化具有零值的数组:

unsigned int bit_arr[MAXBYTES] = { };

然后数组的每个成员都将保持零值。另外,尽量避免诸如printf("\n");. putchar('\n')如果您一次只需要输出一个符号,最好使用它。

此外,如果您不会在此翻译单元之外使用此例程,您可以将static关键字添加到您的工作函数原型和定义中,该函数除外。main

于 2013-08-17T18:17:06.650 回答