[ create a new paste ] login | about

Link: http://codepad.org/JSwe8ez4    [ raw code | output | fork ]

C, pasted on Nov 16:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define ISBITSET(x, i) (( x[i>>3] & (1<<(i&7)) ) != 0)
#define SETBIT(x, i) x[i>>3] |= (1<<(i&7));
#define CLEARBIT(x, i) x[i>>3] &= (1<<(i&7)) ^ 0xFF;

long sumPrimes(long n) {
        char b[(n+1)/8+1];
        int i, p;
        long sum = 0;

        memset(b, 255, sizeof(b));

        for (p=2; p<=n; p++){
            if (ISBITSET(b,p)){
                sum += p;
                CLEARBIT(b,p);
                for (i=p*p; i<=n; i+=p){
                    CLEARBIT(b,i);
                }
            }
        }

        return sum;
    }

void main(void)
{
    printf("%ld", sumPrimes(2000000));
}


Output:
1
Segmentation fault


Create a new paste based on this one


Comments: