46 lines
753 B
C
46 lines
753 B
C
#include <math.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "primes.h"
|
|
|
|
#define MAX_NUM 100000000
|
|
#define TRUE 1
|
|
#define FALSE 0
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
int i, j, match, midpoint;
|
|
long sum = 0;
|
|
char *primes;
|
|
|
|
primes = malloc((MAX_NUM+1)*sizeof(char));
|
|
for (i=1; i<MAX_NUM+1; i++) {
|
|
if (is_prime(i)) {
|
|
primes[i] = TRUE;
|
|
} else {
|
|
primes[i] = FALSE;
|
|
}
|
|
}
|
|
|
|
for(i=1; i<MAX_NUM; i++) {
|
|
match = TRUE;
|
|
if (!(primes[i+1])) continue;
|
|
|
|
midpoint = (int)sqrt(i) + 1;
|
|
for (j=1; j<midpoint; j++) {
|
|
if ((!(i%j)) && (!primes[j+i/j])) {
|
|
match = FALSE;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (match) {
|
|
sum += i;
|
|
}
|
|
}
|
|
|
|
printf("The sum of prime-generating numbers less than %d is %ld\n", MAX_NUM, sum);
|
|
free(primes);
|
|
return 0;
|
|
}
|