Skip to content

Instantly share code, notes, and snippets.

@beyondlimits
Last active May 2, 2023 23:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beyondlimits/c7a2130dff3d2b84942a8f67319b1584 to your computer and use it in GitHub Desktop.
Save beyondlimits/c7a2130dff3d2b84942a8f67319b1584 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LOW_PRIME_COUNT 6542
int main()
{
puts("2\n3");
unsigned primes[LOW_PRIME_COUNT];
memset(primes, 255, sizeof(primes));
primes[0] = 2;
primes[1] = 3;
int index = 2;
unsigned a = 2, a1 = 3, a2 = 9, current = 5; //count = 1;
for (;;) {
int i;
for (i = 2; primes[i] < a1; i++) {
if (!(current % primes[i])) {
goto notaprime;
}
}
if (index < LOW_PRIME_COUNT) {
primes[index] = current;
++index;
}
printf("%u\n", current);
notaprime:
if (current == 0xFFFFFFFD) {
break;
}
current += a;
a ^= 6;
if (current >= a2) {
if (current == a2) {
current += a;
a ^= 6;
}
a2 += a1;
++a1;
a2 += a1;
}
}
exit(EXIT_SUCCESS);
}
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
unsigned *sieve = calloc(1 << 28, 1);
if (!sieve) {
err(EXIT_FAILURE, "Failed to allocate memory for a sieve");
}
puts("2");
unsigned a = 3, b = 0;
do {
if (!(sieve[b >> 5] & (1 << (b & 0x1F)))) {
printf("%d\n", a);
unsigned c = a + b;
do {
sieve[c >> 5] |= 1 << (c & 0x1F);
c += a;
} while (c < 0x80000000);
}
++b;
a += 2;
} while (a < 0x10000);
do {
if (!(sieve[b >> 5] & (1 << (b & 0x1F)))) {
printf("%u\n", a);
}
++b;
a += 2;
} while (a < 0xFFFFFFFF);
free(sieve);
exit(EXIT_SUCCESS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment