-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
35 lines (29 loc) · 691 Bytes
/
main.c
File metadata and controls
35 lines (29 loc) · 691 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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main() {
printf("Calculate primes up to: ");
int max_number;
scanf("%d", &max_number);
if (max_number < 0) {
return 1;
}
if (max_number <= 1) {
return 0;
}
printf("2\n");
bool* non_prime = malloc(max_number + 1);
for (int i = 0; i <= max_number; i++) {
non_prime[i] = false;
}
for (int p = 3; p <= max_number; p += 2) {
if (non_prime[p] == false) {
for (int i = p*p; i <= max_number; i += p) {
non_prime[i] = true;
}
printf("%d\n", p);
}
}
free(non_prime);
return 0;
}