-
Notifications
You must be signed in to change notification settings - Fork 0
/
prime_v3.c
61 lines (49 loc) · 1.59 KB
/
prime_v3.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
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>
//gcc fastprime.c -o fastprime -lm
int main(int varc, char *vars[]) {
int *prime;
int end_prime, n = 1, high, lines = 0;
prime = (int *)malloc((n+1)*sizeof(int));
prime[0] = 2;
lines++;
if( varc > 1 ) {
end_prime = atoi(vars[1]);
} else {
printf("End at:");
scanf("%d", &end_prime);
//end_prime = 100000;
}
high = (int)sqrt(end_prime)+1;
printf("%d ", prime[0]);
int i, j, flag = 1;
for(i = 3; i <= end_prime; i++) {
flag = 1; // reset flag
if(i % 2 == 0) // if it's divisible by 2, then it's not a prime
continue; // so, just move on to the next number
// divide i by all current prime numbers
for(j = 0; j < n; j++) {
if(i % prime[j] == 0) { // if it's divisible by a previous prime number,
flag = 0; // change flag so it does not get added to the prime array
continue; // if we found a number for which the mod is 0 then just skip to the next number
}
}
// if flag remains at 1 we found a new prime
if(flag == 1) {
if(i < high){
n++;
prime = (int *)realloc(prime, (n+1)*sizeof(int));
prime[n-1] = i;
}
printf("%d ", i);
lines++;
if(lines == 10) {
printf("\n");
lines = 0;
}
}
}
return 0;
}