Python: Combination Functions (Micropython)
This is a set of combination functions. Programmed with a Casio fx-CG 100 but should work on any calculator with Python.
The script is freeware.
Functions included:
fact(n): Factorial of the non-negative integer n. Some math modules include a factorial function, like Numworks. This function will be used in all the functions on this script.
ncr(n,r): Combination
npr(n,r): Permutation
nhr(n,r): Combination, repetitions are allowed
catalan(n): nth Catalan Number
narayana(n,k): Narayana Number (n, k are positive integers)
binpdf(n,k,p): Binomial Probability:
n: trials
k: number of successes
p: probability of success
bndcdf(n,k,p): Cumulative Binomial Probability – Lower Tail
n: trials
k: number of successes (from 0 to k)
p: probability of success
Script: combo.py
'''
combinatorics 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
Eddie
All original content copyright, © 2011-2026. Edward Shore. Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited. This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.