Last active
December 11, 2015 06:48
-
-
Save joriki/4561437 to your computer and use it in GitHub Desktop.
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment