Skip to content

Problem 121 #4261

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

Merged
merged 3 commits into from
Mar 20, 2021
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,8 @@
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_119/sol1.py)
* Problem 120
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_120/sol1.py)
* Problem 121
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_121/sol1.py)
* Problem 123
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_123/sol1.py)
* Problem 125
Expand Down
Empty file.
64 changes: 64 additions & 0 deletions project_euler/problem_121/sol1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""
A bag contains one red disc and one blue disc. In a game of chance a player takes a
disc at random and its colour is noted. After each turn the disc is returned to the
bag, an extra red disc is added, and another disc is taken at random.
The player pays £1 to play and wins if they have taken more blue discs than red
discs at the end of the game.
If the game is played for four turns, the probability of a player winning is exactly
11/120, and so the maximum prize fund the banker should allocate for winning in this
game would be £10 before they would expect to incur a loss. Note that any payout will
be a whole number of pounds and also includes the original £1 paid to play the game,
so in the example given the player actually wins £9.
Find the maximum prize fund that should be allocated to a single game in which
fifteen turns are played.
Solution:
For each 15-disc sequence of red and blue for which there are more red than blue,
we calculate the probability of that sequence and add it to the total probability
of the player winning. The inverse of this probability gives an upper bound for
the prize if the banker wants to avoid an expected loss.
"""

from itertools import product


def solution(num_turns: int = 15) -> int:
"""
Find the maximum prize fund that should be allocated to a single game in which
fifteen turns are played.
>>> solution(4)
10
>>> solution(10)
225
"""
total_prob: float = 0.0
prob: float
num_blue: int
num_red: int
ind: int
col: int
series: tuple[int, ...]

for series in product(range(2), repeat=num_turns):
num_blue = series.count(1)
num_red = num_turns - num_blue
if num_red >= num_blue:
continue
prob = 1.0
for ind, col in enumerate(series, 2):
if col == 0:
prob *= (ind - 1) / ind
else:
prob *= 1 / ind

total_prob += prob

return int(1 / total_prob)


if __name__ == "__main__":
print(f"{solution() = }")