Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@joriki
Created October 24, 2012 09:14
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/3945010 to your computer and use it in GitHub Desktop.
Save joriki/3945010 to your computer and use it in GitHub Desktop.
Count the number of binary sequences with repeating initial segment; see http://math.stackexchange.com/questions/219552
public class Question219552 {
public static void main (String [] args) {
for (int n = 2;;n += 2) {
int n2 = n >> 1;
long count = 0;
for (long bits = 0;bits < 1L << n;bits++) {
outer:
for (int l = 1;l <= n2;l++) {
for (int bit = 0;bit < l;bit++)
if (((bits >> bit) & 1) != ((bits >> (bit + l)) & 1))
continue outer;
count++;
break;
}
}
System.out.println (n + " & " + count + " & " + ((1L << n) - count) + " & " + count / (double) (1L << n) + "\\\\");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment