[New-bugs-announce] [issue35431] The math module should provide a function for computing binomial coefficients
kellerfuchs
report at bugs.python.org
Thu Dec 6 16:18:02 EST 2018
New submission from kellerfuchs <kellerfuchs at hashbang.sh>:
A recuring pain point, for me and for many others who use Python for mathematical computations, is that the standard library does not provide a function for computing binomial coefficients.
I would like to suggest adding a function, in the math module, with the following signature:
binomial(n: Integral, k: Integral) -> Integral
A simple native Python implementation would be:
from functools import reduce
from math import factorial
from numbers import Integral
def binomial(n: Integral, k: Integral) -> Integral:
if k < 0 or n < 0:
raise ValueError("math.binomial takes non-negative parameters.")
k = min(k, n-k)
num, den = 1, 1
for i in range(k):
num = num * (n - i)
den = den * (i + 1)
return num//den
As far as I can tell, all of the math module is implemented in C, so this should be done in C too, but the implemented behaviour should be equivalent.
I will submit a Github pull request once I have a ready-to-review patch.
Not starting a PEP, per [PEP 1]:
> Small enhancements or patches often don't need a PEP and can be injected into the Python development workflow with a patch submission to the Python issue tracker.
[PEP 1]: https://www.python.org/dev/peps/pep-0001/#id36
----------
messages: 331251
nosy: kellerfuchs
priority: normal
severity: normal
status: open
title: The math module should provide a function for computing binomial coefficients
type: enhancement
versions: Python 3.8
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35431>
_______________________________________
More information about the New-bugs-announce
mailing list