Created
December 12, 2023 13:17
-
-
Save carlos-adir/c1128c8c1bb29cdb10e421cb8e367524 to your computer and use it in GitHub Desktop.
Light binomial coefficient
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
def comb(a: int, b: int) -> int: | |
""" | |
Implements the binomial coefficient: | |
( a ) a ! | |
( ) = ------------- | |
( b ) b! * (a-b)! | |
""" | |
prod = 1 | |
for i in range(min(b, a-b)): | |
prod *= a-i | |
prod /= i+1 | |
return prod |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From https://stackoverflow.com/a/58442370