Skip to content

Instantly share code, notes, and snippets.

@robbie-cao
Created November 29, 2016 10:25
Show Gist options
  • Save robbie-cao/435350f4e5bfc889fe97cc66d87809ac to your computer and use it in GitHub Desktop.
Save robbie-cao/435350f4e5bfc889fe97cc66d87809ac to your computer and use it in GitHub Desktop.
W(n, k)
% function S=stirling(n,k)
%
% The number of ways of partitioning a set of n elements
% into k nonempty sets is called a Stirling set number.
% For example, the set {1,2,3} can
% be partitioned into three subsets in one way:
% {{1},{2},{3}};
% into two subsets in three ways:
% {{1,2},{3}}, {{1,3},{2}}, and {{1},{2,3}};
% and into one subset in one way:
% {{1,2,3}}.
function S = stirling2(n, k)
S = 0;
for i = 0:k
S = S + (-1)^i * factorial(k) / (factorial(i) * factorial(k-i)) * (k-i)^n;
end
S = S / factorial(k);
end
% step 1
vector_factors = factor(n);
num_of_factors = length(vector_factors);
% step 2
num_stirling2 = stirling2(num_of_factors, k);
% step 3
[a,b] = histc(vector_factors,unique(vector_factors));
count_of_occurence = a(b);
result = num_stirling2;
for i = 1:length(count_of_occurence)
result = result / factorial(count_of_occurence(i));
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment