#include #define bitsPerInt 32U static int checkNum(unsigned, unsigned[]); void setBit(unsigned, unsigned[]); int main() { const int numsToTest = 320; unsigned primeSet[10]; // Space for 320 bits int totalPrimes = 0, toTest, idx; for (idx = 0; idx < 10; idx++) primeSet[idx] = 0; for (toTest = 2; toTest < numsToTest; toTest++) if (checkNum(toTest, primeSet)) totalPrimes++; printf("Found %d primes:", totalPrimes); for (toTest = 0; toTest < numsToTest; toTest++) if (primeSet[toTest/bitsPerInt] >> (toTest % bitsPerInt) & 0x1) printf(" %d", toTest); printf("\n"); return 0; } static int checkNum(unsigned toTest, unsigned primeSet[]) { int divisor; for (divisor = 2; divisor < toTest && toTest % divisor != 0; divisor++) ; if (divisor == toTest) setBit(toTest, primeSet); return divisor == toTest; } void setBit(unsigned prime, unsigned primeSet[]) { primeSet[prime/bitsPerInt] = primeSet[prime/bitsPerInt] | (1 << prime % bitsPerInt); }