Skip to content

Instantly share code, notes, and snippets.

@hackjoy
Created February 8, 2013 02:05
Show Gist options
  • Select an option

  • Save hackjoy/4736045 to your computer and use it in GitHub Desktop.

Select an option

Save hackjoy/4736045 to your computer and use it in GitHub Desktop.
Calculates the number of group combinations possible given a number n and a number of sets k.
def stirling(n, k):
if k > n:
return 0
elif k == n or k == 1:
return 1
else:
return (k*stirling(n-1, k) + stirling(n-1, k-1))
def bell(n):
bell = 0
for sets in range(1, n+1):
bell += stirling(n, sets)
return bell
# If we try to split into more groups than we have people - it is not possible. If k and n are equal there is only one way to do it
# The formula for calculating the Stirling numbers is
# S(n, k) = k*S(n-1, k) + S(n-1, k-1)
# The Bell number B(n) is the number of ways of splitting n into any number of parts from 0 to n
# B(n) is the sum of S(n,k) for k =1,2, ... , n.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment