forked from dimpeshmalviya/C-Language-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsieve.c
More file actions
37 lines (31 loc) · 857 Bytes
/
sieve.c
File metadata and controls
37 lines (31 loc) · 857 Bytes
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
// filename: sieve.c
// Compile: gcc -o sieve sieve.c
// Run: ./sieve 50
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s <n>\n", argv[0]);
return 1;
}
int n = atoi(argv[1]);
if (n < 2) {
printf("No primes <= %d\n", n);
return 0;
}
bool *isPrime = malloc((n+1) * sizeof(bool));
for (int i = 0; i <= n; ++i) isPrime[i] = true;
isPrime[0] = isPrime[1] = false;
for (int p = 2; p * p <= n; ++p) {
if (isPrime[p]) {
for (int multiple = p * p; multiple <= n; multiple += p)
isPrime[multiple] = false;
}
}
printf("Primes <= %d:\n", n);
for (int i = 2; i <= n; ++i) if (isPrime[i]) printf("%d ", i);
printf("\n");
free(isPrime);
return 0;
}