public
Last active

Search for permutations of objects in a circle for which any pair of objects is at a different minimal distance along the circle than originally; see http://math.stackexchange.com/questions/281094.

  • Download Gist
Question281094.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
public class Question281094 {
static int m;
static int [] p;
static int count;
 
public static void main (String [] args) {
for (m = 2;;m++) {
p = new int [m];
p [0] = 1;
count = 0;
recurse (2);
System.out.println (m + " : " + count);
}
}
 
static void recurse (int n) {
if (n > m) {
count++;
return;
}
 
outer:
for (int i = 1;i < m;i++)
if (p [i] == 0) {
for (int j = 0;j < m;j++)
if (p [j] != 0) {
int d1 = Math.abs (j - i);
int d2 = Math.abs (p [j] - n);
if (d1 > m / 2)
d1 = m - d1;
if (d2 > m / 2)
d2 = m - d2;
if (d1 == d2)
continue outer;
}
p [i] = n;
recurse (n + 1);
p [i] = 0;
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.