Sieve of Eratosthenes
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char* argv[]) {
int limit = 10000;
int calculate = limit * 20;
bool numbers[calculate];
memset(&numbers, true, sizeof(bool) * calculate);
for (int i = 2; i < calculate; i++) {
if (numbers[i]) {
for (int j = i * i; j < calculate; j += i) {
numbers[j] = false;
}
if (i * i > calculate) {
break;
}
}
}
int sum = 0;
int counter = 1;
for (int i = 2; i < calculate; i++) {
if (numbers[i]) {
sum += i;
if (counter >= limit) {
break;
}
counter++;
}
}
printf("sum of first %d primes is: %d\n", counter, sum);
return 0;
}
Source:
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes