forked from superjamie/snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sieve.c
61 lines (49 loc) · 1.64 KB
/
sieve.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
51
52
53
54
55
56
57
58
59
60
61
/* Sieve of Eratosthenes
*
* A method to calculate prime numbers
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int main (int argc, char *argv[])
{
/* limit = the amount of numbers to check are prime or not */
int limit, i, j;
if (argc != 2 || ((limit = atoi(argv[1]))) <= 0) {
printf("Usage: %s NUMBERS\n", argv[0]);
exit(1);
}
limit = limit-1;
/* allocate an array to store numbers
* we can't store this on the stack because it could get huge! */
int *numbers;
numbers = (int *)malloc(sizeof(int)*limit);
if (!numbers) {
perror("Could not allocate memory");
exit(1);
}
/* fill the array with all numbers to check */
for(i = 0; i < limit; i++) {
numbers[i] = i+2;
}
for (i = 0; i < limit; i++) {
/* if we've already crossed this one out, skip it */
if (numbers[i] != 0) {
/* square the number, if it's smaller than the limit, find its array address and zero it */
for (j = 2 * numbers[i]-2; j < limit; j += numbers[i])
numbers[j]=0;
}
}
/* one is always prime */
printf("1 ");
/* walk the number array. if the number is not zero, print it */
for (i = 0; i < limit; i++) {
if (numbers[i] != 0) {
printf("%d ", numbers [i]);
}
}
/* clean up */
printf("\n");
free(numbers);
return 0;
}