From 74d64cb3a870093dc7e79dd056a12bd961796363 Mon Sep 17 00:00:00 2001 From: Simon Kiefhaber Date: Sun, 24 Oct 2021 01:59:04 +0200 Subject: [PATCH 1/3] Added solution for problem 493 --- project_euler/problem_493/__init__.py | 0 project_euler/problem_493/sol1.py | 53 +++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 project_euler/problem_493/__init__.py create mode 100644 project_euler/problem_493/sol1.py diff --git a/project_euler/problem_493/__init__.py b/project_euler/problem_493/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_493/sol1.py b/project_euler/problem_493/sol1.py new file mode 100644 index 000000000000..22a276653018 --- /dev/null +++ b/project_euler/problem_493/sol1.py @@ -0,0 +1,53 @@ +""" +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 cominations +[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) -> float: + """ + Calculates the expected number of distinct colors + + >>> f'{solution(10):.9f}' + '5.669644129' + + >>> f'{solution(30):.9f}' + '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 result + + +if __name__ == "__main__": + print(f"{solution(20):.9f}") From 3a6a130390f3a3509fbd02dff9610ccea6ceada4 Mon Sep 17 00:00:00 2001 From: Simon Kiefhaber Date: Sun, 24 Oct 2021 02:01:41 +0200 Subject: [PATCH 2/3] fixed typo --- project_euler/problem_493/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_493/sol1.py b/project_euler/problem_493/sol1.py index 22a276653018..8c6d06d3f34a 100644 --- a/project_euler/problem_493/sol1.py +++ b/project_euler/problem_493/sol1.py @@ -33,7 +33,7 @@ def solution(num_picks: int = 20) -> float: """ - Calculates the expected number of distinct colors + Calculates the expected number of distinct colours >>> f'{solution(10):.9f}' '5.669644129' From e43deae19f4fd5ba45628ab18dfaede2fcb0be02 Mon Sep 17 00:00:00 2001 From: Simon Kiefhaber Date: Tue, 26 Oct 2021 14:34:21 +0200 Subject: [PATCH 3/3] return result as string --- project_euler/problem_493/sol1.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/project_euler/problem_493/sol1.py b/project_euler/problem_493/sol1.py index 8c6d06d3f34a..c9879a528230 100644 --- a/project_euler/problem_493/sol1.py +++ b/project_euler/problem_493/sol1.py @@ -31,14 +31,14 @@ NUM_BALLS = BALLS_PER_COLOUR * NUM_COLOURS -def solution(num_picks: int = 20) -> float: +def solution(num_picks: int = 20) -> str: """ Calculates the expected number of distinct colours - >>> f'{solution(10):.9f}' + >>> solution(10) '5.669644129' - >>> f'{solution(30):.9f}' + >>> solution(30) '6.985042712' """ total = math.comb(NUM_BALLS, num_picks) @@ -46,8 +46,8 @@ def solution(num_picks: int = 20) -> float: result = NUM_COLOURS * (1 - missing_colour / total) - return result + return f"{result:.9f}" if __name__ == "__main__": - print(f"{solution(20):.9f}") + print(solution(20))