Skip to content

Instantly share code, notes, and snippets.

@joriki
Created January 16, 2023 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joriki/c185f14930dd67f66de765bc59e45204 to your computer and use it in GitHub Desktop.
Save joriki/c185f14930dd67f66de765bc59e45204 to your computer and use it in GitHub Desktop.
Compute the probability that a random permutation of the 54 stickers on a Rubik's cube includes no adjacent squares of the same colour by simulation; see https://math.stackexchange.com/questions/4618350.
public class Question4618350a {
final static int ntrials = 1000000000;
public static void main(String [] args) {
int count = 0;
outer:
for (int n = 0;n < ntrials;n++) {
int [] p = Permutations.getRandomPermutation(54);
for (int square = 0;square < 6;square++) {
for (int x = 0;x < 2;x++)
for (int y = 0;y < 3;y++)
if (color (p,square,x,y) == color (p,square,x + 1,y))
continue outer;
for (int x = 0;x < 3;x++)
for (int y = 0;y < 2;y++)
if (color (p,square,x,y) == color (p,square,x,y + 1))
continue outer;
}
count++;
}
System.out.println(count / (double) ntrials);
}
static int color (int [] p,int square,int x,int y) {
return p [9 * square + 3 * y + x] % 6;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment