def factorial(n):
    """ Return the factorial (n!) of the number n. """
    if n < 2:
        return 1
    product = 1
    num = n
    for i in xrange(n):
        product *= num
        num -= 1
    return product

def bin_coeff(n, k):
    """ Return the binominal Coefficient of n over k """
    if k < 0 or k > n:
        return 0
    else:
        return factorial(n) / (factorial(k) * factorial(n - k))

def B(n,p,k):
    """ 
        Return the Binomial distribution with length n, probability p and k 
        successes 
    """
    binom = bin_coeff(n, k)
    a = p**k
    b = (1-p)**(n-k)
    return binom*a*b

def B_sum(n,p,i,j):
    """ Return the sum from i to j of the Binomial distributions """
    summe = 0
    for k in xrange(i,j + 1):
        summe += B(n,p,k)
    return summe

print B_sum(1000,(1.0/6),134,200)
