Skip to content

Instantly share code, notes, and snippets.

@maksverver
Created May 4, 2014 22:29
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 maksverver/013261076082f2165b67 to your computer and use it in GitHub Desktop.
Save maksverver/013261076082f2165b67 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import numpy as np
N = 22 # number of prisoners
M = 2 # number of switches
def state(n,m):
# n is the number of uncounted prisoners
# m is the value of the counter
return (n << M) + m
P = np.identity(N<<M)
for n in range(N):
for m in range(1<<M):
s = state(n,m)
if m < n and (m + 1) < (1<<M):
t = state(n,m+1)
p = (n - m)/N
P[s,t] += p
P[s,s] -= p
t = state(n-m,0)
p = 1/N
P[s,t] += p
P[s,s] -= p
# https://en.wikipedia.org/wiki/Absorbing_Markov_chain
R = np.matrix(np.identity(N<<M) - P)[1:, 1:,].getI()
print(R[state(N-1,0) - 1].sum())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment