Skip to content

Instantly share code, notes, and snippets.

@joriki
Created December 19, 2012 00:48
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/4333467 to your computer and use it in GitHub Desktop.
Save joriki/4333467 to your computer and use it in GitHub Desktop.
Calculate probabilities of breaking even or winning at least the expected value at some point in a series of bets; see http://math.stackexchange.com/questions/261357.
public class Question261357 {
final static boolean even = false;
final static int percent = 74;
final static double p = percent / 100.;
final static int win = 7;
final static int lose = 6;
static double [] [] cache;
static boolean [] [] done;
public static void main (String [] args) {
for (int n = 1;;n++) {
cache = new double [n] [n];
done = new boolean [n] [n];
System.out.println (" " + n + " : " + p (n,0,0));
}
}
static double p1 (int n,int w,int k) {
int e = w * win - (k - w) * lose;
return (even ? e >= 0 : 100 * e >= k * (percent * win - (100 - percent) * lose)) ? 1 : n == 0 ? 0 : p (n,w,k);
}
static double p (int n,int w,int k) {
if (!done [k] [w]) {
cache [k] [w] = p * p1 (n - 1,w + 1,k + 1) + (1 - p) * p1 (n - 1,w,k + 1);
done [k] [w] = true;
}
return cache [k] [w];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment