blob: 90e0bbbd7e5177b7fd7ea815d0e02cb92cf40f35 (
plain)
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
|
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// The number to find primes up to
int n = 2000000;
// The number of primes in the primes array
int s = 0;
int *listPrimes(int num){
int i;
int *primes;
int *sieve = (int *) malloc(num * sizeof(int));
//initialize to all 1s (except 0 and 1 which are not prime)
for(i = 2; i < num; i++)
sieve[i] = 1;
for(i = 2; i < ceil(sqrt(num)); i++){
if(sieve[i] == 1){
int j = i * i;
while(j < num){
sieve[j] = 0;
j += i;
}
}
}
//now check which were prime
primes = (int *) malloc(sizeof(int));
for(i = 2; i < num; i++){
if(sieve[i]){
primes[s] = i;
s++;
primes = (int *) realloc (primes, (s + 1) * sizeof(int));
}
}
return primes;
}
int main(){
int *p = listPrimes(n);
long long sum = 0;
int len = s;
for(int i = 0; i < s; i++){
sum += p[i];
}
printf("%ld\n", sum);
return 0;
}
|