Skip to content

Instantly share code, notes, and snippets.

@piotr-piatkowski
Last active November 14, 2022 23:14
Show Gist options
  • Save piotr-piatkowski/a32aa6853031c31fca1f8915f9e8b55a to your computer and use it in GitHub Desktop.
Save piotr-piatkowski/a32aa6853031c31fca1f8915f9e8b55a to your computer and use it in GitHub Desktop.
#include <stdio.h>
#define LEN 1200000000
char nums[LEN];
int main() {
nums[0] = 1;
nums[1] = 1;
long primes_count = 0;
long last_prime = -1;
long i = 2;
while( i < LEN ) {
if( i < 100 || (i < 1000000 && (i % 1000) == 0) || i % 1000000 == 0) {
printf("%ld\n", i);
}
if( nums[i] == 0 ) {
primes_count += 1;
last_prime = i;
long j = 2 * i;
while(j < LEN) {
nums[j] = 1;
j += i;
}
}
i += 1;
}
printf("last_prime=%ld, primes_count=%ld\n", last_prime, primes_count);
return 0;
}
#!/usr/bin/python3
import math
LEN=1200000000
nums = bytearray(LEN)
nums[0] = 1
nums[1] = 1
primes_count = 0
last_prime = None
i = 2
while i < LEN:
if i < 100 or (i < 1000000 and i % 1000 == 0) or i % 1000000 == 0:
print(i)
if nums[i] == 0:
primes_count += 1
last_prime = i
j = 2 * i
while j < LEN:
nums[j] = 1
j += i
i += 1
print(f"{last_prime=}, {primes_count=}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment