aboutsummaryrefslogtreecommitdiff
path: root/src/talks/wasm/examples/4-primes-sieve/primes.c
blob: 3f86864662f4e9f70eb4596097b1f53b1697bfa5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdlib.h>
#include <string.h>

int count(int n) {
	if (n < 2)
		return 0;

	/* Prepare sieve array */
	char *notprime = malloc(n+2);
	memset(notprime, 0, n+2);

	int count = 0;
	for (int i = 2; i < n; i++) {
		count += 1 - notprime[i];
		for (int j = 2*i; j < n; j += i)
			notprime[j] = 1;
	}

	/* Manual memory management is fun :D */
	free(notprime);

	return count;
}

Generated with cgit - Back to sebastiano.tronto.net