fork download
  1. import java.util.Arrays;
  2. public class Main{
  3. public static void main(String[] args){
  4.  
  5. double[][] probabilities;
  6. int numMines = 4; //The number of mines you have
  7. double firstProbability = .5; //The probability that a mine has gold at first. 50% in example problem. Express as a number between 0 and 1
  8. double probabilityAfterFound = .9; //The probability that a mine has gold once one gold has been found. 90% in example problem.
  9.  
  10. probabilities = new double[numMines + 1][numMines+1];
  11. probabilities[0][0]=1;
  12. for(int mine = 1; mine <= numMines; mine++){
  13. probabilities[mine][0] = probabilities[mine-1][0]*(1-firstProbability);
  14. probabilities[mine][1] = probabilities[mine-1][0]*(firstProbability) + probabilities[mine-1][1]*(1-probabilityAfterFound);
  15. for(int numGolds = 2; numGolds <= numMines; numGolds++){
  16. probabilities[mine][numGolds] = probabilities[mine-1][numGolds-1]*probabilityAfterFound + probabilities[mine-1][numGolds]*(1-probabilityAfterFound);
  17. }
  18. }
  19.  
  20. for(int mine = 0; mine <= numMines; mine++){
  21. System.out.println(Arrays.toString(probabilities[mine]));
  22. }
  23.  
  24. double expectedGolds = 0;
  25. for(int numGolds = 0; numGolds<=numMines; numGolds++){
  26. expectedGolds+=numGolds*probabilities[numMines][numGolds];
  27. }
  28. System.out.println("You expect to find gold " + expectedGolds + " times.");
  29. System.out.println("You expect to find gold at least once " + ((1-probabilities[numMines][0])*100) + "% of the time");
  30. }
  31. }
Success #stdin #stdout 0.11s 320256KB
stdin
Standard input is empty
stdout
[1.0, 0.0, 0.0, 0.0, 0.0]
[0.5, 0.5, 0.0, 0.0, 0.0]
[0.25, 0.3, 0.45, 0.0, 0.0]
[0.125, 0.155, 0.315, 0.405, 0.0]
[0.0625, 0.078, 0.171, 0.324, 0.36450000000000005]
You expect to find gold 2.85 times.
You expect to find gold at least once 93.75% of the time