Skip to content

Add Project Euler problem 100 solution 1 #8175

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
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
3 changes: 3 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@
## Electronics
* [Builtin Voltage](electronics/builtin_voltage.py)
* [Carrier Concentration](electronics/carrier_concentration.py)
* [Circular Convolution](electronics/circular_convolution.py)
* [Coulombs Law](electronics/coulombs_law.py)
* [Electric Conductivity](electronics/electric_conductivity.py)
* [Electric Power](electronics/electric_power.py)
Expand Down Expand Up @@ -936,6 +937,8 @@
* [Sol1](project_euler/problem_097/sol1.py)
* Problem 099
* [Sol1](project_euler/problem_099/sol1.py)
* Problem 100
* [Sol1](project_euler/problem_100/sol1.py)
* Problem 101
* [Sol1](project_euler/problem_101/sol1.py)
* Problem 102
Expand Down
Empty file.
48 changes: 48 additions & 0 deletions project_euler/problem_100/sol1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""
Project Euler Problem 100: https://projecteuler.net/problem=100
If a box contains twenty-one coloured discs, composed of fifteen blue discs and
six red discs, and two discs were taken at random, it can be seen that
the probability of taking two blue discs, P(BB) = (15/21) x (14/20) = 1/2.
The next such arrangement, for which there is exactly 50% chance of taking two blue
discs at random, is a box containing eighty-five blue discs and thirty-five red discs.
By finding the first arrangement to contain over 10^12 = 1,000,000,000,000 discs
in total, determine the number of blue discs that the box would contain.
"""


def solution(min_total: int = 10**12) -> int:
"""
Returns the number of blue discs for the first arrangement to contain
over min_total discs in total
>>> solution(2)
3
>>> solution(4)
15
>>> solution(21)
85
"""

prev_numerator = 1
prev_denominator = 0

numerator = 1
denominator = 1

while numerator <= 2 * min_total - 1:
prev_numerator += 2 * numerator
numerator += 2 * prev_numerator

prev_denominator += 2 * denominator
denominator += 2 * prev_denominator

return (denominator + 1) // 2


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