Skip to content

Instantly share code, notes, and snippets.

@joriki
Created April 23, 2016 22:49
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/52ea56822e473bb101b42a51c1dfbbe6 to your computer and use it in GitHub Desktop.
Save joriki/52ea56822e473bb101b42a51c1dfbbe6 to your computer and use it in GitHub Desktop.
Estimate the expected number of steps required to span F_2^n using a certain randomised procedure; see http://math.stackexchange.com/questions/1754859.
import java.util.Random;
public class Question1754859 {
final static int ntrials = 100000000;
final static Random random = new Random ();
final static double p = 0.5;
final static int [] code = {
1,2,4,3,6,7,5
};
public static void main (String [] args) {
double sum1 = 0;
double sum2 = 0;
double sum3 = 0;
double [] sums = new double [6];
int [] counts = new int [6];
for (int n = 0;n < ntrials;n++) {
int first = random.nextInt (7);
do {
first++;
first %= 7;
sum1++;
} while (random.nextDouble () < p);
int second = first;
do {
second++;
second %= 7;
sum2++;
} while (random.nextDouble () < p || second == first);
int index = (second - first + 6) % 7;
counts [index]++;
int third = second;
do {
third++;
third %= 7;
sum3++;
sums [index]++;
} while (random.nextDouble () < p || third == first || third == second || code [third] == (code [first] ^ code [second]));
}
sum1 /= ntrials;
sum2 /= ntrials;
sum3 /= ntrials;
double p6 = Math.pow (p,6);
double p7 = Math.pow (p,7);
double v1 = 1 / (1 - p);
double v2 = (1 - p7) / ((1 - p) * (1 - p6));
double v3 = ((((((((p + 2) * p + 1) * p + 4) * p + 5) * p + 4) * p + 1) * p + 2) * p + 1) / ((1 - p6) * (1 + p * p));
double v = ((((((((2 * p + 4) * p + 4) * p + 8) * p + 9) * p + 8) * p + 5) * p + 4) * p + 3) / ((1 - p6) * (1 + p * p));
System.out.println (sum1 + " / " + v1);
System.out.println (sum2 + " / " + v2);
System.out.println (sum3 + " / " + v3);
System.out.println ();
System.out.println ((sum1 + sum2 + sum3) + " / " + v + " " + (v1 + v2 + v3));
System.out.println ();
for (int i = 0;i < 6;i++)
System.out.println (i + " : " + sums [i] / counts [i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment