Skip to content

Instantly share code, notes, and snippets.

@joriki
Created April 10, 2012 08:31
Show Gist options
  • Save joriki/2349368 to your computer and use it in GitHub Desktop.
Save joriki/2349368 to your computer and use it in GitHub Desktop.
Code to count card combinations according to this math.SE question: http://math.stackexchange.com/questions/129580
import java.util.ArrayList;
import java.util.List;
public class Question129580 {
static List<int []> getPermutations (int n) {
return getPermutations (n,n);
}
static List<int []> getPermutations (int n,int k) {
List<int []> permutations = new ArrayList<int[]> ();
makePermutations (permutations,new int [k],new boolean [n],0);
return permutations;
}
static void makePermutations (List<int []> permutations,int [] permutation,boolean [] used,int j) {
if (j == permutation.length)
permutations.add (permutation.clone ());
else
for (int i = 0;i < used.length;i++)
if (!used [i]) {
permutation [j] = i;
used [i] = true;
makePermutations (permutations,permutation,used,j + 1);
used [i] = false;
}
}
public static void main (String [] args) {
for (int n = 1;n <= 4;n++) {
int k = (n * (n + 1)) / 2;
int count = 0;
outer:
for (int [] p : Permutations.getPermutations (k)) {
for (int i = 0;i < n;i++) {
for (int j = (i * (i + 1)) / 2,l = 0;l < i;l++,j++) {
if (Math.abs (p [j] - p [j + 1]) == 1)
continue outer;
}
}
count++;
}
System.out.println (n + " : " + count);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment