Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Binomial Coefficient

import math x = int(input("Enter a value for x: ")) y = int(input("Enter a value for y: "))  if y == 1 or y == x:     print(1)  if y > x:     print(0)         else:     a = math.factorial(x)     b = math.factorial(y)     div = a // (b*(x-y))     print(div)   

This binomial coefficient program works but when I input two of the same number which is supposed to equal to 1 or when y is greater than x it is supposed to equal to 0.

like image 902
jcoke Avatar asked Oct 25 '14 08:10

jcoke


People also ask

How do you write a binomial coefficient in Python?

Use the math. comb() Function to Calculate the Binomial Coefficient in Python. The comb() function from the math module returns the combination of the given values, which essentially has the same formula as the binomial coefficient. This method is an addition to recent versions of Python 3.8 and above.

How is the binomial coefficient calculated?

The binomial coefficients are the integers calculated using the formula: (nk)=n!k! (n−k)!. The binomial theorem provides a method for expanding binomials raised to powers without directly multiplying each factor: (x+y)n= nΣk=0 (nk) xn−kyk. Use Pascal's triangle to quickly determine the binomial coefficients.

How do you find n choose k in Python?

Definition and UsageThe math. comb() method returns the number of ways picking k unordered outcomes from n possibilities, without repetition, also known as combinations. Note: The parameters passed in this method must be positive integers.

What is a binomial coefficient?

In mathematics, the binomial coefficients are the positive integers that occur as coefficients in the binomial theorem. Commonly, a binomial coefficient is indexed by a pair of integers n ≥ k ≥ 0 and is written.


1 Answers

This question is old but as it comes up high on search results I will point out that scipy has two functions for computing the binomial coefficients:

  1. scipy.special.binom()
  2. scipy.special.comb()

    import scipy.special  # the two give the same results  scipy.special.binom(10, 5) # 252.0 scipy.special.comb(10, 5) # 252.0  scipy.special.binom(300, 150) # 9.375970277281882e+88 scipy.special.comb(300, 150) # 9.375970277281882e+88  # ...but with `exact == True` scipy.special.comb(10, 5, exact=True) # 252 scipy.special.comb(300, 150, exact=True) # 393759702772827452793193754439064084879232655700081358920472352712975170021839591675861424 

Note that scipy.special.comb(exact=True) uses Python integers, and therefore it can handle arbitrarily large results!

Speed-wise, the three versions give somewhat different results:

num = 300  %timeit [[scipy.special.binom(n, k) for k in range(n + 1)] for n in range(num)] # 52.9 ms ± 107 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)  %timeit [[scipy.special.comb(n, k) for k in range(n + 1)] for n in range(num)] # 183 ms ± 814 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)each)  %timeit [[scipy.special.comb(n, k, exact=True) for k in range(n + 1)] for n in range(num)] # 180 ms ± 649 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 

(and for n = 300, the binomial coefficients are too large to be represented correctly using float64 numbers, as shown above).

like image 85
Shaun Coutts Avatar answered Sep 23 '22 17:09

Shaun Coutts