Skip to content

Instantly share code, notes, and snippets.

@igorvanloo
Created May 21, 2022 15:00
Show Gist options
  • Save igorvanloo/07f3051e4e7d0ccb8daa0f6185d64b38 to your computer and use it in GitHub Desktop.
Save igorvanloo/07f3051e4e7d0ccb8daa0f6185d64b38 to your computer and use it in GitHub Desktop.
p267
def winnings(x, f):
return pow(1 + 2*f, x) * pow(1 - f, 1000 - x)
def find_f():
#Finds the optimal f
f = 0.001
goal = 10**9
step = 0.001 #Adjust this for greater accuracy
best_f, corresponding_x = 0, 1000
while f < 0.5:
x = 1
while winnings(x, f) < goal:
x += 1 #Adjust this for greater accuracy
if x < corresponding_x:
best_f, corresponding_x = f, x
f += step
return best_f, corresponding_x
def n_choose_r(n, r): # nCr function
if r > n:
return "n must be greter than r"
else:
return int(math.factorial(n) / (math.factorial(r) * math.factorial(n - r)))
def compute():
total = 0
for h in range(432, 1001):
total += n_choose_r(1000, h)
return round(total/pow(2, 1000), 12)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment