Best permutation count algorithm in ruby

Viewed 228

I am trying to count the number of numbers that are expressed by P number of 1's and 0's in binary form. If P = 2, then the numbers expressed are 0011, 1100, 0110, 0101, 1001, 1010, so the count is 6.

I tried:

[0,0,1,1].permutation.to_a.uniq

But it is not the best solution for big numbers (P can be <= 30).

What could be the best permutation technique, or do we have any straight forward math to do this?

1 Answers
Related