# A miner is sent to a mine, with probability 1/2, the miner is trapped # When a miner is trapped, there are 3 doors in the mine # The first door leads to a tunnel that will take him to safety after 3 hours # The second door leads to a tunnel that returns him to the mine after 5 hours # The third door leads to a tunnel that returns him to the mine after 7 hours # Compute the expected time to reach safety for n independent times # Let X_i be the random variable representing the miner escape time in the ith time and let X = X_1 + ... + X_n # We are asking for E(X) # Use the law of total expectation to calculate E(X_i), let A be the event that the miner is trapped then # E(X_i) = E(X_i|A)*P(A) + 0*P(not A) = E(X_i|A)*1/2. Now use again the law of total expectation to calculate E(X_i|A), # let B be the event that the miner reaches safety in the first try, then we have # E(X_i|A) = E((X_i|A)|B)*P(B) + E((X_i|A)| not B)*P(not B) = 3*1/3 + (5*1/2 + 7*1/2 + E(X_i|A))*2/3 = 5 + 2/3*E(X_i|A) # E(X_i|A) = 15 # By linearity of expectation, we get E(X) = E(X_1) + ... + E(X_n) = 15*1/2*n = 7.5*n if n > 0, 0 otherwise def f(): var i, flag, n i = 0 while i < n: prob(1,1): flag = 1 while flag > 0: prob(1,2): flag = 0 tick 3 else: prob(1,1): flag = 1 tick 5 else: flag = 1 tick 7 i = i + 1