Skip to content

Commit

Permalink
Problem 60 (prime pairs)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pete Bevin committed Aug 16, 2013
1 parent f06d0de commit 98df81c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
49 changes: 49 additions & 0 deletions eu060.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "euler.h"

static int primes[100000];
static int length;

static int prime_pair(int a, int b) {
char string[50];
int n;

sprintf(string, "%d%d", primes[a], primes[b]);
n = atoi(string);
if (!is_prime(n)) return 0;

sprintf(string, "%d%d", primes[b], primes[a]);
n = atoi(string);
if (!is_prime(n)) return 0;

return 1;
}

void eu060(char *ans) {
length = genprimes(primes, 10000);

int a, b, c, d, e;

for (a = 0; a <= length-5; a++) {
for (b = a; b <= length-4; b++) {
if (!prime_pair(a, b)) continue;
for (c = b; c <= length-3; c++) {
if (!prime_pair(a, c)) continue;
if (!prime_pair(b, c)) continue;
for (d = c; d <= length-2; d++) {
if (!prime_pair(a, d)) continue;
if (!prime_pair(b, d)) continue;
if (!prime_pair(c, d)) continue;

for (e = d; e <= length-1; e++) {
if (!prime_pair(a, e)) continue;
if (!prime_pair(b, e)) continue;
if (!prime_pair(c, e)) continue;
if (!prime_pair(d, e)) continue;
sprintf(ans, "%d", primes[a] + primes[b] + primes[c] + primes[d] + primes[e]);
return;
}
}
}
}
}
}
1 change: 1 addition & 0 deletions euler.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ struct puzzle puzzles[] = {
{ "057", &eu057, "153" },
{ "058", &eu058, "26241" },
{ "059", &eu059, "107359" },
{ "060", &eu060, NULL },
};

#define NPUZZLES (sizeof puzzles / sizeof(puzzles[0]))
Expand Down

0 comments on commit 98df81c

Please sign in to comment.