Created
November 23, 2024 10:34
-
-
Save mrphlip/f0f6276d843daa792ffbf9faca78517f to your computer and use it in GitHub Desktop.
xkcd 3015 - D Combinatorics
This file contains hidden or 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/python | |
from collections import defaultdict | |
from fractions import Fraction | |
from functools import reduce | |
from math import comb | |
def lhs(): | |
return Fraction(comb(5, 2), comb(10, 2)) | |
def rhs(): | |
dice = [d(6)] * 3 + [d(4)] | |
probs = reduce(convolve, dice) | |
return sum(p for n,p in probs.items() if n >= 16) | |
def d(n): | |
return {i: Fraction(1,n) for i in range(1, n+1)} | |
def convolve(a, b): | |
res = defaultdict(lambda: Fraction(0)) | |
for n1, p1 in a.items(): | |
for n2, p2 in b.items(): | |
res[n1 + n2] += p1 * p2 | |
return res | |
l = lhs() | |
print(f"Chance of drawing arrows: {float(l*100):.02f}% ({l})") | |
r = rhs() | |
print(f"Chance of rolling dice: {float(r*100):.02f}% ({r})") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment