Search code examples
algorithmmathlanguage-agnosticpermutationnumber-theory

How many distinct digit permutations exist for a specific N-digit number?


The problem is simple. We have an N-digit digit number (N <= 18) and we need to know all the possible distinct combinations of this number. For example the answer for the number 214 (N = 3) is 6 (You can create 214, 241, 124, 142, 412, 421). I'm looking for an algorithm that calculates this.

I noticed that for an N-digit number the maximum amount of different numbers is equal to N!. However I have no clue on how this thing works when the digit are not distinct. I tried several methods for example trying to write a formula to make it or code but nothing worked.


Solution

  • It would be

    N! / (#0! * #1! * #2! * #3! * #4! * .. * #9!)
    

    with #d number of d.

    N! is total number of permutation of distinct element.

    consider then 1a, 1b as disting 1, we have to divide N! by permutation number of 1 (and also for other numbers).

    So for 1112235 it would be

    7! / (3! * 2! * 1! * 1!) (ignoring 0! which is 1)