combo.py

Created by ews31415

Created on April 13, 2026

891 Bytes

Combinatoric Functions


'''
combonatorics 4/12/2026
Edward Shore

round to nearest integer:
int(f+.5)
'''

from math import *

# factorial positive integers
def fact(n):
  f,i=1,1
  while i<=n:
    f*=i
    i+=1
  return int(f+.5)

# a lot of functions will use fact

# combination
def ncr(n,r):
  f=fact(n)/(fact(r)*fact(n-r))
  return int(f+.5)

# permutation
def npr(n,r):
  f=fact(n)/fact(n-r)
  return int(f+.5)

# combination w/repetitions
def nhr(n,r):
  f=fact(n+r-1)/(fact(r)*fact(n-1))
  return int(f+.5)

# catalan numbers
def catalan(n):
  f=fact(2*n)/(fact(n)**2*(n+1))
  return int(f+.5)

# narayana numbers
def narayana(n,k):
  f=1/n*ncr(n,k)*ncr(n,k-1)
  return int(f+.5)

# binomial probability
def binpdf(n,k,p):
  # p=prob
  f=ncr(n,k)*p**k*(1-p)**(n-k)
  return f

# bin lower tail
def bincdf(n,k,p):
  # 0 to k, p=prob
  s=0
  for i in range(k+1):
    s+=binpdf(n,i,p)
  return s

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.