-
Notifications
You must be signed in to change notification settings - Fork 0
/
primes.c
192 lines (156 loc) · 4.25 KB
/
primes.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#define FALSE 0
#define TRUE (!FALSE)
#define RATE 50000
// Iterator implementation
typedef struct _node *Node;
typedef struct _node {
uint32_t value;
Node next;
} node_t;
typedef struct _iterator {
Node start;
Node curr;
Node end;
} iterator_t;
typedef iterator_t *Iterator;
// Iterator interface
Node newNode (uint32_t value);
Iterator newIterator (void);
void destroyIterator (Iterator iterator);
void resetIterator (Iterator iterator);
int isEndIterator (Iterator iterator);
uint32_t nextIterator (Iterator iterator);
void pushIterator (Iterator iterator, uint32_t value);
// Count primes using an iterator for found primes
void countPrimes(Iterator iter, uint32_t max);
// Calculate progress as a double
double percentProgress (uint32_t step, uint32_t total);
int main (int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s max_prime\n", argv[0]);
exit(EXIT_FAILURE);
}
int32_t max = atoi(argv[1]);
if (max <= 0) {
errx(EXIT_FAILURE, "max_prime must be a positive integer");
}
Iterator iter = newIterator();
if (iter != NULL) {
countPrimes(iter, (uint32_t)max);
} else {
errx(EXIT_FAILURE, "Failed to create iterator");
}
destroyIterator(iter);
return EXIT_SUCCESS;
}
Node newNode (uint32_t value) {
Node node = calloc(1, sizeof(node_t));
if (node != NULL) {
node->value = value;
node->next = NULL;
}
return node;
}
Iterator newIterator (void) {
Iterator iterator = calloc(1, sizeof(iterator_t));
if (iterator != NULL) {
iterator->start = NULL;
iterator->curr = NULL;
iterator->end = NULL;
}
return iterator;
}
void destroyIterator (Iterator iterator) {
if (iterator != NULL) {
// Free nodes
Node curr, next;
curr = iterator->start;
while (curr != NULL) {
next = curr->next;
free(curr);
curr = next;
}
// Free iterator
free(iterator);
}
}
void resetIterator (Iterator iterator) {
if (iterator != NULL) {
iterator->curr = iterator->start;
}
}
int isEndIterator (Iterator iterator) {
if (iterator == NULL) {
errx(EXIT_FAILURE, "Cannot isEnd on null iterator");
}
return (iterator->curr == NULL);
}
uint32_t nextIterator (Iterator iterator) {
if (iterator == NULL) {
errx(EXIT_FAILURE, "Cannot next on null iterator");
}
if (iterator->curr == NULL) {
errx(EXIT_FAILURE, "Cannot next at end of iterator");
}
uint32_t currVal = iterator->curr->value;
iterator->curr = iterator->curr->next;
return currVal;
}
void pushIterator (Iterator iterator, uint32_t value) {
if (iterator != NULL) {
Node new = newNode(value);
if (new != NULL) {
if (iterator->start == NULL) {
iterator->start = new;
iterator->curr = new;
} else {
iterator->end->next = new;
}
iterator->end = new;
}
}
}
double percentProgress (uint32_t step, uint32_t total) {
return 100.0 * ((double) step / (double) total);
}
void countPrimes(Iterator iter, uint32_t max) {
uint32_t primes = 0;
uint32_t i;
for (i = 2; i <= max; i++) {
int prime = TRUE;
uint32_t j = 0;
resetIterator(iter);
while (prime && !isEndIterator(iter) && j * j <= i) {
j = nextIterator(iter);
if (!(i % j)) {
prime = FALSE;
}
}
if (prime) {
pushIterator(iter, i);
primes++;
if (primes < 100) {
printf("%3d, ", i);
if (!(primes % 5)) {
printf("\n");
}
} else if (primes == 100) {
printf("%d\n", i);
}
if (!(primes % RATE)) {
printf("\033[GFound %12d at %7.3lf%%...",
primes, percentProgress(i, max)
);
fflush(stdout);
}
}
}
if (primes < 100 && primes % 5) {
printf("\n");
}
printf("\033[G\033[KFound %12d primes.\n", primes);
}