Files
project-euler/problem-357.c
2019-03-18 15:20:29 -04:00

54 lines
871 B
C

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.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;
}
}
*/
primes = eratosthenes(MAX_NUM);
if (primes == NULL) {
printf("Error");
return 1;
}
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;
}