#include #include /* V1: initial version * V2: bug fix: force full unsigned allocation instead of bytes. */ #include "sum_of_multiples.h" #define BITS (sizeof(unsigned)*8) #define getbit(i) (!!(sieve[(i) / BITS] & (1 << ((i) % BITS)))) #define setbit(i) (sieve[(i) / BITS] |= (1 << ((i) % BITS))) /* use a kind of eratosthenes's sieve, to avoid the trivial solution :-) */ unsigned sum(const unsigned *f, const size_t n, const unsigned l) { unsigned sieve_size = (l-1)/BITS+1; unsigned num, i, res=0; unsigned *sieve; sieve = calloc(sieve_size, sizeof(unsigned)); /* l bits array */ if (!sieve) return 0; for (i=0; i2) { last=atol(av[1]); for (i=2; i