sebastiano.tronto.net

Source files and build scripts for my personal website
git clone https://git.tronto.net/sebastiano.tronto.net
Download | Log | Files | Refs | README

primes.c (382B)


      1 #include <stdlib.h>
      2 #include <string.h>
      3 
      4 int count(int n) {
      5 	if (n < 2)
      6 		return 0;
      7 
      8 	/* Prepare sieve array */
      9 	char *notprime = malloc(n+2);
     10 	memset(notprime, 0, n+2);
     11 
     12 	int count = 0;
     13 	for (int i = 2; i < n; i++) {
     14 		count += 1 - notprime[i];
     15 		for (int j = 2*i; j < n; j += i)
     16 			notprime[j] = 1;
     17 	}
     18 
     19 	/* Manual memory management is fun :D */
     20 	free(notprime);
     21 
     22 	return count;
     23 }