-
Notifications
You must be signed in to change notification settings - Fork 0
/
sieve-of-eratosthenes.c
50 lines (42 loc) · 1.01 KB
/
sieve-of-eratosthenes.c
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
40
41
42
43
44
45
46
47
48
49
50
/**
* Implementation of the prime generation algo - Sieve of Eratosthenes
* @author Δοδύο
* @version 0.1
* @changelog
* - V0.1: Basic implementation
*/
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <limits.h>
#define SIZE (UINT_MAX / 80)
char *arr;
void soe (char *a, size_t size) {
size_t i, j, k;
for (i = 2, j = sqrt(size); i <= j; i++) {
if (!arr[i - 1]) continue;
for (k = i * i; k <= size; k += i) {
arr[k - 1] = 0;
}
}
}
int main () {
size_t i, j = 1;
clock_t begin;
// Sieve of Eratosthenes
begin = clock();
arr = (char *) malloc(sizeof(char) * SIZE);
memset(arr, 1, SIZE);
soe(arr, SIZE);
/*for (i = 2; i <= SIZE; i++) {
if (arr[i - 1]) {
printf("%d ", i);
if (!(j++ % 10)) {
printf("\n");
}
}
}
printf("\n");*/
printf("\nTime spent: %lfsecs\n", (double)(clock() - begin) / CLOCKS_PER_SEC);
return 0;
}