Created
January 14, 2024 18:27
-
-
Save lvaughn/42766c2ae8b8875148cfad0a5f8e0ad5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Answer to the Fiddler On the Proof newsletter for Jan 12, 2024 | |
from fractions import Fraction | |
from itertools import combinations | |
dice_values = [4, 6, 8, 10, 12, 20] | |
# Part 1 | |
probs = [] | |
for pair in combinations(dice_values, 2): | |
# The probability of both getting the same value is 1/<larger of the two dice> | |
probs.append(Fraction(1, max(pair))) | |
result = sum(probs)/len(probs) | |
print("Part 1", result, float(result)) | |
# Part 2 | |
probs = [] | |
for triple in combinations(dice_values, 3): | |
# The probability is the 1/<product of the two largest dice sizes> | |
in_order = list(sorted(triple)) | |
probs.append(Fraction(1, in_order[1]*in_order[2])) | |
result = sum(probs)/len(probs) | |
print("Part 2", result, float(result)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment