Skip to content

Instantly share code, notes, and snippets.

@joriki
Created January 20, 2013 15:38
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/4579386 to your computer and use it in GitHub Desktop.
Save joriki/4579386 to your computer and use it in GitHub Desktop.
Calculate probabilities for a variant of blackjack; see http://math.stackexchange.com/questions/282743.
import java.util.Locale;
public class Question282743 {
final static int max = 28;
static boolean [] [] cached = new boolean [max] [max];
static double [] [] p = new double [max] [max];
static double p (int a,int b) {
if (!cached [a] [b]) {
p [a] [b] = calculateP (a,b);
cached [a] [b] = true;
}
return p [a] [b];
}
static double calculateP (int a,int b) {
return
a < b || a > b + 5 ? 0 :
b == 1 ? 1/6. :
p (a,b - 1) + p (b - 1,b - 1) / 6;
}
public static void main (String [] args) {
for (int b = 16;b <= 21;b++) {
double sum = 0;
for (int a = b;a <= 21;a++)
for (int c = 22;c <= a + 6;c++)
sum += p (a,b) * p (c,a + 1);
System.out.printf (Locale.ENGLISH,"%d&%.5f\\\\\n",b,sum);
}
double pPlayer21 = p (21,19);
double pDealer21 = p(19,19) * p(21,20) + p(20,19) * p(21,21);
System.out.println (pPlayer21);
System.out.println (pDealer21);
System.out.println (pPlayer21 + pDealer21);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment