mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-22 11:10:14 +00:00
895dffb412
* [pre-commit.ci] pre-commit autoupdate updates: - [github.com/astral-sh/ruff-pre-commit: v0.0.291 → v0.0.292](https://github.com/astral-sh/ruff-pre-commit/compare/v0.0.291...v0.0.292) - [github.com/codespell-project/codespell: v2.2.5 → v2.2.6](https://github.com/codespell-project/codespell/compare/v2.2.5...v2.2.6) - [github.com/tox-dev/pyproject-fmt: 1.1.0 → 1.2.0](https://github.com/tox-dev/pyproject-fmt/compare/1.1.0...1.2.0) * updating DIRECTORY.md * Fix typos in test_min_spanning_tree_prim.py * Fix typos * codespell --ignore-words-list=manuel --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com> Co-authored-by: Christian Clauss <cclauss@me.com>
54 lines
1.4 KiB
Python
54 lines
1.4 KiB
Python
"""
|
|
Project Euler Problem 493: https://projecteuler.net/problem=493
|
|
|
|
70 coloured balls are placed in an urn, 10 for each of the seven rainbow colours.
|
|
What is the expected number of distinct colours in 20 randomly picked balls?
|
|
Give your answer with nine digits after the decimal point (a.bcdefghij).
|
|
|
|
-----
|
|
|
|
This combinatorial problem can be solved by decomposing the problem into the
|
|
following steps:
|
|
1. Calculate the total number of possible picking combinations
|
|
[combinations := binom_coeff(70, 20)]
|
|
2. Calculate the number of combinations with one colour missing
|
|
[missing := binom_coeff(60, 20)]
|
|
3. Calculate the probability of one colour missing
|
|
[missing_prob := missing / combinations]
|
|
4. Calculate the probability of no colour missing
|
|
[no_missing_prob := 1 - missing_prob]
|
|
5. Calculate the expected number of distinct colours
|
|
[expected = 7 * no_missing_prob]
|
|
|
|
References:
|
|
- https://en.wikipedia.org/wiki/Binomial_coefficient
|
|
"""
|
|
|
|
import math
|
|
|
|
BALLS_PER_COLOUR = 10
|
|
NUM_COLOURS = 7
|
|
NUM_BALLS = BALLS_PER_COLOUR * NUM_COLOURS
|
|
|
|
|
|
def solution(num_picks: int = 20) -> str:
|
|
"""
|
|
Calculates the expected number of distinct colours
|
|
|
|
>>> solution(10)
|
|
'5.669644129'
|
|
|
|
>>> solution(30)
|
|
'6.985042712'
|
|
"""
|
|
total = math.comb(NUM_BALLS, num_picks)
|
|
missing_colour = math.comb(NUM_BALLS - BALLS_PER_COLOUR, num_picks)
|
|
|
|
result = NUM_COLOURS * (1 - missing_colour / total)
|
|
|
|
return f"{result:.9f}"
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print(solution(20))
|