Skip to content

Instantly share code, notes, and snippets.

@jacquesattack
Created July 31, 2018 18:35
Show Gist options
  • Save jacquesattack/b5644fcef5d81ad887631a40e7d995ff to your computer and use it in GitHub Desktop.
Save jacquesattack/b5644fcef5d81ad887631a40e7d995ff to your computer and use it in GitHub Desktop.
Calculate Bell numbers.
# https://en.wikipedia.org/wiki/Partition_of_a_set#Counting_partitions
bell = function(n) {
ifelse(n > 0,
sum(Vectorize(function(n, k) {
choose(n - 1, k) * bell(k)
}, "k")(n, 0:(n - 1))),
1)
}
bell(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment