blob: 7dd4816f9db524cc663ded138d3ee489964f6d5e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
#include <stdio.h>
#include <stdlib.h>
void sieve(int n)
{
if (n <= 2)
{
return;
}
// Generate array
int *a = calloc(n, sizeof(int));
int count = 0;
// Actual sieve and count
for (int i = 2; i < n; i++)
{
if (a[i] == 0)
{
for (int k = 2 * i; k < n; k += i)
{
a[k] = 1;
}
}
}
for (int i = 2; i < n; i++)
{
if (a[i] == 0)
{
count++;
}
}
// Print the count
printf("%d\n", count);
free(a);
}
|