Skip to content

Instantly share code, notes, and snippets.

@joriki
Created February 26, 2013 07:34
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/5036693 to your computer and use it in GitHub Desktop.
Save joriki/5036693 to your computer and use it in GitHub Desktop.
Perform an exhaustive search for a binary (n,k,q) code; see http://math.stackexchange.com/questions/314645.
public class Question314645 {
static int n,k,q;
static int [] bitCounts;
public static void main (String [] args) {
n = Integer.parseInt (args [0]);
k = Integer.parseInt (args [1]);
q = Integer.parseInt (args [2]);
bitCounts = new int [1 << n];
for (int i = 0;i < bitCounts.length;i++)
for (int j = 0;j < n;j++)
if ((i & (1 << j)) != 0)
bitCounts [i]++;
recurse (new int [k],1);
}
static void recurse (int [] code,int index) {
if (index == k) {
for (int word : code) {
System.out.print (" ");
for (int i = 0;i < n;i++)
System.out.print ((word >> i) & 1);
System.out.println ();
}
System.exit (0);
}
outer:
for (code [index] = code [index - 1] + 1;code [index] < 1 << n;code [index]++) {
for (int i = 0;i < index;i++)
if (bitCounts [code [i] ^ code [index]] < q)
continue outer;
recurse (code,index + 1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment