Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9118231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:05:48+00:00 2026-06-17T05:05:48+00:00

I wrote the following code to list all the prime numbers upto 2 billion

  • 0

I wrote the following code to list all the prime numbers upto 2 billion using Sieve’s method. I used bitmasking for flagging purpose. While I am able to get the prime numbers correctly, a few primes in the beginning are missing every time. Please help me find the bug in the program.

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

#define MAX 2000000000

char* listPrimes(){
int block = sqrt(MAX);
char* mark = calloc((MAX/8),sizeof(char));
int i = 2;
int j;
char mask[8];
for(j=0;j<8;j++)
    mask[j] = 0;
mask[7] = 1;
mask[6] |= mask[7] << 1;
mask[5] |= mask[7] << 2;
mask[4] |= mask[7] << 3;
mask[3] |= mask[7] << 4;
mask[2] |= mask[7] << 5;
mask[1] |= mask[7] << 6;
mask[0] |= mask[7] << 7;

for(j=0;j<8;j++)
    printf("%d ",mask[j]);
mark[0] |= mask[0];
mark[0] |= mask[1];

while (i < block){

        for (j = 2; i*j <= block; j++)
                mark[(i*j) / 8] |= mask[((i*j) % 8 )];
        i++;
    }
printf("\n");
printf("The block size is\t:\t%d\n",block);


j = 2;
while(j<=block){
    if((mark[j / 8] & mask[j]) == 0 ){
        for(i = 2;i <= MAX; i++){
            if((i%j) == 0){
                mark[i / 8] |= mask[(i % 8)];
            }
        }
    }
while((mark[++j / 8] & mask[j % 8]) != 0);
}


for(j=0;j<=MAX;j++)
        if((mark[j / 8] & mask[(j % 8)]) == 0)
            printf("%d\n", ((8*(j / 8)) + (j % 8)));

return mark;
}   

int main(int argc,char* argv[]){

listPrimes();

return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:05:50+00:00Added an answer on June 17, 2026 at 5:05 am

    As ArunMK said, in the second while loop you mark the prime j itself as a multiple of j. And as Lee Meador said, you need to take the modulus of j modulo 8 for the mask index, otherwise you access out of bounds and invoke undefined behaviour.

    A further point where you invoke undefined behaviour is

    while((mark[++j / 8] & mask[j % 8]) != 0);
    

    where you use and modify j without intervening sequence point. You can avoid that by writing

    do {
        ++j;
    }while((mark[j/8] & mask[j%8]) != 0);
    

    or, if you insist on a while loop with empty body

    while(++j, (mark[j/8] & mask[j%8]) != 0);
    

    you can use the comma operator.

    More undefined behaviour by accessing mark[MAX/8] which is not allocated in

    for(i = 2;i <= MAX; i++){
    

    and

    for(j=0;j<=MAX;j++)
    

    Also, if char is signed and eight bits wide,

    mask[0] |= mask[7] << 7;
    

    is implementation-defined (and may raise an implementation-defined signal) since the result of

    mask[0] | (mask[7] << 7)
    

    (the int 128) is not representable as a char.

    But why are you dividing each number by all primes not exceeding the square root of the bound in the second while loop?

        for(i = 2;i <= MAX; i++){
            if((i%j) == 0){
    

    That makes your algorithm not a Sieve of Eratosthenes, but a trial division.

    Why don’t you use the technique from the first while loop there too? (And then, why two loops at all?)

    while (i <= block){
        if ((mark[i/8] & mask[i%8]) == 0) {
            for (j = 2; i*j < MAX; j++) {
                mark[(i*j) / 8] |= mask[((i*j) % 8 )];
            }
        }
        i++;
    }
    

    would not overflow (for the given value of MAX, if that is representable as an int), and produce the correct output orders of magnitude faster.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have wrote following code for a simple problem in Python - def Peu1(numbers):
I wrote some code using generics and I got into the following situation I
I'm using the following code to export all the emails in a specific gmail
i wrote following code to create a linkbutton programmatically, but its showing like lable
I wrote following code...but i am getting Error like: Error 1 'LoginDLL.Class1.Login(string, string, string)':
I wrote the following code for text file encryption and decryption. The encryption process
I wrote the following code to select text from database,but when i echo the
if wrote the following code and do not understand why the trace returns false:
I wrote the following code: import java.lang.*; import DB.*; private Boolean validateInvoice(String i) {
I wrote the following code snippet in VS2010: #pragma once #include stdafx.h #ifndef SECURITY_WIN32

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.