Skip to content

Instantly share code, notes, and snippets.

@joriki
Created September 5, 2018 23:59
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/83f6dd92d5e4b70a01975f7948e9766f to your computer and use it in GitHub Desktop.
Save joriki/83f6dd92d5e4b70a01975f7948e9766f to your computer and use it in GitHub Desktop.
Compute the probability for a two-dimensional random walk with duplication and vanishing to produce at least one duplicate at the origin; see https://math.stackexchange.com/questions/2906613.
public class Question2906613 {
final static int maxn = 1026;
final static int [] [] ds = {
{0,1},
{-1,0},
{0,-1},
{1,0}
};
public static void main (String [] args) {
double [] [] a = new double [maxn + 1] [maxn + 1];
a [0] [0] = 1;
for (int n = 1;n <= maxn;n += n) {
double s = Double.POSITIVE_INFINITY;
for (;;) {
double sum = 0;
for (int x = 0;x < n;x++)
for (int y = 0;y < n;y++)
if (x + y != 0) {
double die = 1;
for (int [] d : ds)
die *= 1 - a [Math.abs (x + d [0])] [Math.abs (y + d [1])] / 4;
sum += die;
a [x] [y] = 1 - die;
}
if (sum >= s)
break;
s = sum;
}
System.out.println (n + " " + (1 - Math.pow (1 - a [0] [1] / 4,4)));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment