-
Notifications
You must be signed in to change notification settings - Fork 0
/
billionaire.py
41 lines (30 loc) · 1.31 KB
/
billionaire.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# project euler #267
# You are given a unique investment opportunity.
# Starting with £1 of capital, you can choose a fixed proportion, f, of your capital to bet
# on a fair coin toss repeatedly for 1000 tosses.
# Your return is double your bet for heads and you lose your bet for tails.
# For example, if f = 1/4, for the first toss you bet £0.25, and if heads comes up
# you win £0.5 and so then have £1.5. You then bet £0.375 and if the second toss is tails,
# you have £1.125.
# Choosing f to maximize your chances of having at least £1,000,000,000 after 1,000 flips, what is
# the chance that you become a billionaire?
# All computations are assumed to be exact (no rounding), but give your answer rounded to 12 digits
# behind the decimal point in the form 0.abcdefghijkl.
from fractions import *
from decimal import *
import random
def gamble():
acct = Decimal('1')
rounds = 10000
proportion = Decimal('0.5')
for i in range(rounds):
if random.choice([True, False]):
acct += (acct * proportion) * 2
print(f"acct after win: {acct}")
else:
acct -= (acct * proportion)
print(f"acct after loss: {acct}")
print(acct)
# gamble()
# well, by tweaking the number, it's easy to see that you don't bet more than half, which makes sense.
# I assume the next is to simlify this into a particular probability.