coef.py

Created by bilouclic

Created on October 10, 2024

1.1 KB


def factorielle(n):
    if n <= 1 : return 1
    else : return n * factorielle(n-1)

def binomial_fact(n, k):
    assert k >= 0 and n >= 0
    return factorielle(n)/(factorielle(k)*factorielle(n-k))

def binomial_pascal (n, k):
    assert k >= 0 and n >= 0
    if k == n or k==0:
        return 1
    elif k>n:
        return 0
    else:
        return binomial_pascal(n-1,k)+binomial_pascal(n-1,k-1)

def binomial_capitaine(n,k):
    assert k >= 0 and n >= 0
    if k == n or k==0:
        return 1
    elif k>n:
        return 0
    else:
        return binomial_capitaine(n-1,k-1)*n/k

if __name__ == "__main__" :
    assert factorielle(4) == 24 and factorielle(6) == 720 and factorielle(-2) 
    == 1
    assert binomial_fact(2,1) == 2 and binomial_fact(4,2) == 6 and binomial_fact
    (5,3) == 10 and binomial_fact(6,3) == 20
    assert binomial_pascal(2,1) == 2 and binomial_pascal(4,2) == 6 and 
    binomial_pascal(5,3) == 10 and binomial_pascal(6,3) == 20
    assert binomial_capitaine(2,1) == 2 and binomial_capitaine(4,2) == 6 and 
    binomial_capitaine(5,3) == 10 and binomial_capitaine(6,3) == 20








During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.