Created
September 20, 2021 09:32
-
-
Save madmann91/833cccca7f477119e5379e1bcab20943 to your computer and use it in GitHub Desktop.
Prime number generator for hash tables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdint.h> | |
#include <stdbool.h> | |
static bool is_prime(size_t i) { | |
if (i <= 1) | |
return false; | |
if (i <= 3) | |
return true; | |
if (i % 3 == 0 || i % 2 == 0) | |
return false; | |
for (size_t j = 5; j * j < i; j += 6) { | |
if (i % j == 0 || i % (j + 2) == 0) | |
return false; | |
} | |
return true; | |
} | |
static void gen_primes(size_t min, size_t max) { | |
size_t i = 7; | |
while (i < max) { | |
while (!is_prime(i) && i < max) i++; | |
if (i == max) | |
break; | |
printf("%zu\n", i); | |
min = i; | |
i = i * 2; | |
// Detect overflow | |
if (i < min) | |
break; | |
} | |
} | |
int main() { | |
gen_primes(7, SIZE_MAX); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment