/* Compute prime numbers using Sieve of Eratosthenes and find the smallest difference between two primes whose sum is 1 billion. */ #include #include /* maximum size of primes and it's square root plus 1*/ #define MAXP 1000000000 #define MAXPSQR 31623 /* info about characters for my i386 machine */ #define CHAR_BIT 8 /* bits in a character */ /* mask to turn on odd bits and turn off even bits */ #define CHAR_BIT_ODD_MASK 0xaa #define MIDP MAXP / 2 /* The middle of the list */ #define MAXC MAXP / CHAR_BIT + 2 /* number of chars for bit map */ #define BITMASK(b) (1 << ((b) % CHAR_BIT)) #define BITSLOT(b) ((b) / CHAR_BIT) #define BITSET(a, b) ((a)[BITSLOT(b)] |= BITMASK(b)) #define BITCLEAR(a, b) ((a)[BITSLOT(b)] &= ~BITMASK(b)) #define BITTEST(a, b) ((a)[BITSLOT(b)] & BITMASK(b)) int main() { int j,k; int jmin,jmax; /* this array has one bit for each number up to MAXP */ static unsigned char map[MAXC]; printf("Find the smallest difference between" " two primes whose sum is %d\n", MAXP); /* clear map of primes. Assume odd numbers are prime except 2 */ for (j=0; j