Saturday, March 14, 2020

[ucycjspi] n^n / n!

Writing code to run through all permutations of n objects can be a pain.  Simpler is to try all possibilities of the n items in each of the n slots.  How much wasted effort is this?  Turns out, quite a bit.  Below is the multiplicative factor of work between the two:

n (n^n/n!)
1 1.0
2 2.0
3 4.5
4 10.7
5 26.0
6 64.8
7 163.4
8 416.1
9 1067.6
10 2755.7
11 7147.7
12 18613.9

By Stirling's approximation, the ratio is approximately (e^n)/sqrt(2 * pi * n).

Maybe the wasted effort is acceptable if you have a relatively quick way of filtering out the non-permutations, i.e., an item occurs in more than one slot.

No comments :