diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-11-22 15:34:30 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-11-22 15:42:12 +0100 |
| commit | a4d6defa1722d52bf0f5bb2f3abf1443c4b6509c (patch) | |
| tree | fc6bda84afeca68fe84d2e56119db107e2c509f1 /src/talks/wasm/examples/4-primes-sieve/primes.c | |
| parent | e855546ab1df31131b906d6cabed91409daf922d (diff) | |
| download | sebastiano.tronto.net-a4d6defa1722d52bf0f5bb2f3abf1443c4b6509c.tar.gz sebastiano.tronto.net-a4d6defa1722d52bf0f5bb2f3abf1443c4b6509c.zip | |
Added talk
Diffstat (limited to 'src/talks/wasm/examples/4-primes-sieve/primes.c')
| -rw-r--r-- | src/talks/wasm/examples/4-primes-sieve/primes.c | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/talks/wasm/examples/4-primes-sieve/primes.c b/src/talks/wasm/examples/4-primes-sieve/primes.c new file mode 100644 index 0000000..3f86864 --- /dev/null +++ b/src/talks/wasm/examples/4-primes-sieve/primes.c | |||
| @@ -0,0 +1,23 @@ | |||
| 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 | } | ||
