Created
January 16, 2023 23:40
-
-
Save joriki/b83894dc407d212b1bfe2f1e4a1cd6a7 to your computer and use it in GitHub Desktop.
Find binary numbers that generate all n-bit binary numbers using at most one XOR; see https://math.stackexchange.com/questions/4611907.
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
import java.util.Random; | |
public class Question4611907 { | |
final static int k = 52; | |
final static int n = 10; | |
final static int m = 1 << n; | |
final static Random random = new Random(); | |
static int count; | |
static int [] counts = new int [m]; | |
static int [] x = new int [k]; | |
static void increment (int x) { | |
if (counts [x]++ == 0) | |
count++; | |
} | |
static void decrement (int x) { | |
if (--counts [x] == 0) | |
count--; | |
} | |
static void clear (int i) { | |
decrement (x [i]); | |
for (int j = 0;j < k;j++) | |
if (j != i) | |
decrement (x [i] ^ x [j]); | |
} | |
static void set (int i) { | |
increment (x [i]); | |
for (int j = 0;j < k;j++) | |
if (j != i) | |
increment (x [i] ^ x [j]); | |
} | |
static void randomize (int i) { | |
x [i] = random.nextInt(m); | |
} | |
public static void main (String [] args) { | |
increment (0); | |
for (int i = 0;i < k;i++) { | |
randomize (i); | |
increment (x [i]); | |
for (int j = 0;j < i;j++) | |
increment (x [i] ^ x [j]); | |
} | |
int l = 0; | |
int max = 0; | |
for (double beta = 0;count < m;beta += .00000001) { | |
for (int i = 0;i < k;i++) { | |
int oldCount = count; | |
int oldX = x [i]; | |
clear (i); | |
randomize (i); | |
set (i); | |
if (count < oldCount && Math.random() > Math.exp(beta * (count - oldCount))) { | |
clear (i); | |
x [i] = oldX; | |
set (i); | |
} | |
max = Math.max(max,count); | |
} | |
if ((++l & 0x7fff) == 0) { | |
System.out.println(beta + " : " + max + " / " + count); | |
} | |
} | |
for (int i = 0;i < k;i++) { | |
System.out.write((i & 7) == 0 ? '\n' : ' '); | |
System.out.print(String.format("%" + n + "s", Integer.toBinaryString(x [i])).replace(' ', '0')); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment