Given a clock, rearrange six consecutive numbers on its face, so
the sum of every pair of adjacent numbers is a prime.
In C. Same approach as Charlie generally - with same result. I ripped the exhaustive permutation algorithm off inet.
Would it be good to have black out times where people cant post for a while so it is not sooo first come first solve? But I guess that is part of flooble. The shark feeding frenzy.
result
clock 1 2 3 4 9 10 7 6 5 8 11 12
clock 1 2 3 4 7 10 9 8 5 6 11 12
prog
#include <stdio.h>
int level = -1;
int start = 0;
int notPrime(int sum) {
int primes[10] = {1,2,3,5,7,11,13,17,19,23}, i;
for(i = 0; i < 10; i++)
if (sum == primes[i])
return 0;
return 1;
}
int clkPrime(int *v) {
int clk[12] = {0,};
int i, temp;
/*
printf("clkPrime() : %d %d %d %d %d %d start %d\n",
v[0], v[1], v[2], v[3], v[4], v[5], start);
*/
//init clk to normal clock
for (i = 0; i < 12; i++)
clk[i] = i + 1;
//set renumbered six
for (i = 0; i < 6; i ++) {
temp = start + i;
if (temp > 11)
temp -= 12;
clk[temp] = v[i] + start;
if ( (clk[temp] > 11) )
clk[temp] -= 12;
}
for ( i = 0; i < 11; i ++)
if ( (notPrime(clk[i] + clk[i+1])) )
return 0;
if ( (notPrime(clk[11] + clk[0])) )
return 0;
else
return 1;
}
void print(const int *v)
{
int i, s[12], j;
for (i = 0; i < 12; i ++)
s[i] = i + 1;
j = start;
for (i = 0; i < 6; i ++) {
j = start + i;
if (j > 11)
j -= 12;
s[j] = v[i] + start;
if (s[j] > 12)
s[j] -= 12;
}
printf("clock %d %d %d %d %d %d %d %d %d %d %d %d \n",
s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], s[8], s[9], s[10], s[11]);
}
void visit(int *Value, int k)
{
int i;
level++;
Value[k] = level;
if ( (Value[k] == 6 ) ) {
if ( (clkPrime(Value)) )
print(Value);
}
else
for (i = 0; i < 6; i++)
if (!(Value[i]) )
visit(Value, i);
level--;
Value[k] = 0;
}
main()
{
int Value[6], i;
for (start = 0; start < 12; start++) {
for (i = 0; i < 6; i++)
Value[i] = 0;
visit(Value, 0);
}
}