Skip to content

Commit 9701e45

Browse files
MaximSmolskiygithub-actionspre-commit-ci[bot]
authored
Add Project Euler problem 100 solution 1 (#8175)
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent b797e43 commit 9701e45

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

Diff for: DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,8 @@
937937
* [Sol1](project_euler/problem_097/sol1.py)
938938
* Problem 099
939939
* [Sol1](project_euler/problem_099/sol1.py)
940+
* Problem 100
941+
* [Sol1](project_euler/problem_100/sol1.py)
940942
* Problem 101
941943
* [Sol1](project_euler/problem_101/sol1.py)
942944
* Problem 102

Diff for: project_euler/problem_100/__init__.py

Whitespace-only changes.

Diff for: project_euler/problem_100/sol1.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
Project Euler Problem 100: https://projecteuler.net/problem=100
3+
4+
If a box contains twenty-one coloured discs, composed of fifteen blue discs and
5+
six red discs, and two discs were taken at random, it can be seen that
6+
the probability of taking two blue discs, P(BB) = (15/21) x (14/20) = 1/2.
7+
8+
The next such arrangement, for which there is exactly 50% chance of taking two blue
9+
discs at random, is a box containing eighty-five blue discs and thirty-five red discs.
10+
11+
By finding the first arrangement to contain over 10^12 = 1,000,000,000,000 discs
12+
in total, determine the number of blue discs that the box would contain.
13+
"""
14+
15+
16+
def solution(min_total: int = 10**12) -> int:
17+
"""
18+
Returns the number of blue discs for the first arrangement to contain
19+
over min_total discs in total
20+
21+
>>> solution(2)
22+
3
23+
24+
>>> solution(4)
25+
15
26+
27+
>>> solution(21)
28+
85
29+
"""
30+
31+
prev_numerator = 1
32+
prev_denominator = 0
33+
34+
numerator = 1
35+
denominator = 1
36+
37+
while numerator <= 2 * min_total - 1:
38+
prev_numerator += 2 * numerator
39+
numerator += 2 * prev_numerator
40+
41+
prev_denominator += 2 * denominator
42+
denominator += 2 * prev_denominator
43+
44+
return (denominator + 1) // 2
45+
46+
47+
if __name__ == "__main__":
48+
print(f"{solution() = }")

0 commit comments

Comments
 (0)