Search code examples
pythonpython-2.7mathbinomial-coefficients

Binomial coefficient and time congestion Propabillity Python


def binomialco(p,k):
  Sum = 1
  Pro = 1
  for i in range(0,k):
    Sum = Sum*(p-i)
  for i in range(1,k+1):
    Pro = Pro*i
  return float(Sum/Pro)

def P0(s,n,v,h):
  athroisma = 0
  for i in range(0,s+1):
    dent= binomialco(n-1,i)*(v*h)**i
    athroisma+=dent
  return athroisma

s=input()
n=input()
v=input()
h=input()

print P0(s,n,v,h)

Problem is when I give as input the following : s=2, n=4, v=float(1/30) and h=3 I am supposed to get 1.33. All i get is 0.0. Can somebody help me?


Solution

  • Guessing from what you are getting, you must be using py2. Here you will have to coerce to float that is you have to return float(Sum)/float(Pro).

    This is a small test to make you understand why

    >>> 1/30
    0
    >>> float(1/30)
    0.0
    >>> float(1)/float(30)
    0.03333333333333333