Add solution for 357
This commit is contained in:
45
problem-357.c
Normal file
45
problem-357.c
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
#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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user