Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem 121 #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Problem 121/Problem 121.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from fractions import Fraction
from math import ceil

def prob_of_win(discs, last_probability, blues_selected, n):
#Chance of blue is 1 / discs
#Multiply by the last probabilty as events are independent as we replace

#Taken n discs out
if discs == n + 1:
#They have won if over half are blue
if blues_selected > n // 2:
return last_probability

#Otherwise this branch was a loss
return 0

#Put another disc in
discs += 1

#Pick a blue
blues_selected += 1
#P(blue) = 1 / discs
blue_last = last_probability * Fraction(1, discs)

#Pick a red, P(red) = (discs - 1) / discs
red_last = last_probability * Fraction(discs - 1, discs)

#Recursively calculate the probability of winning until they reach the maximum number of discs
return prob_of_win(discs, blue_last, blues_selected, n) + prob_of_win(discs, red_last, blues_selected - 1, n)

def solve(turns):
prob = prob_of_win(1, 1, 0, turns)
#P(wins) = 1/x then x = expected number of times to play before winning
#So 1/P(wins) = x so x - 1 is should be the maximum prize pool to at least break even
print("After", turns, "turns, the chance of winning is:", prob, "which is", round(float(prob * 100), 4), "%")
print("The maximum prize fund should be: £", ceil(prob ** -1) - 1, sep = '')

solve(15)