diff options
| -rw-r--r-- | primegen.c | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/primegen.c b/primegen.c new file mode 100644 index 0000000..045c2e5 --- /dev/null +++ b/primegen.c @@ -0,0 +1,69 @@ +#include <stdlib.h> +#include <stdio.h> +#include <stdint.h> +#include <unistd.h> +#include <string.h> + +int main(int argc, char **argv) +{ + int opt; + const char *optstr = "n:"; + long count = -1; + int current, i, primecount = 0; + + while ((opt = getopt(argc, argv, optstr)) != -1) + { + switch (opt) + { + case 'n': + count = strtol(optarg, NULL, 0); + break; + + case '?': + exit(1); + } + } + + if (count <= 0) + { + exit(1); + } + + char *sieve = malloc(sizeof(char) * count); + memset(sieve, 1, sizeof(char) * count); + + current = 2; + + while (current < count) + { + if (sieve[current]) + { + primecount++; + + for (i = current*2; i < count; i+=current) + { + sieve[i] = 0; + } + } + + current++; + } + + printf("const long prime_table[%d] = { 2", primecount); + + for (i = 3; i < count; ++i) + { + if (sieve[i]) + { + printf(",%d", i); + } + } + + printf("\n};\n"); + + printf("const long prime_table_size = %d;\n", primecount); + + free(sieve); + + return 0; +}
\ No newline at end of file |
