Author kellerfuchs
Recipients kellerfuchs
Date 2018-12-06.21:18:01
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1544131082.09.0.788709270274.issue35431@psf.upfronthosting.co.za>
In-reply-to
Content
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
History
Date User Action Args
2018-12-06 21:18:02kellerfuchssetrecipients: + kellerfuchs
2018-12-06 21:18:02kellerfuchssetmessageid: <1544131082.09.0.788709270274.issue35431@psf.upfronthosting.co.za>
2018-12-06 21:18:02kellerfuchslinkissue35431 messages
2018-12-06 21:18:01kellerfuchscreate