First, you don't really need full prime decomposition, only decomposition to primes smaller than your base (I guess you mean 10 here but the problem can be generalized to any base). So we only need factorization into the first 4 primes: 2, 3, 5 and 7. If the rest (prime or not) factor is anything bigger than 1, then the problem has 0 solutions.

Now, lets assume that the number `p`

is factored into:

```
p = 2^d1 * 3^d2 * 5^d3 * 7^d4
```

and is also composed from the `n`

digits:

`p =`

d_{(n-1)}d_{(n-2)}...d_{2}d_{1}d_{0}

Then, rearranging the digits, is will also be:

```
p = 2^q2 * 3^q3 * 4^q4 * 5^q3 * ... * 9^q9
```

where `qi >= 0`

and `q2 + q3 + ... q9 = n`

and also (due to the factorization):

```
for prime=2: d1 = q2 + 2*q4 + q6 + 3*q8
for prime=3: d2 = q3 + q6 + 2*q9
for prime=5: d3 = q5
for prime=7: d4 = q7
```

So the `q5`

and `q7`

are fixed and we have to find all non-negative integer solutions to the equations:

(where the unknowns are the rest `qi`

: `q2, q3, q4, q6, q8 and q9`

)

```
d1 = q2 + 2*q4 + q6 + 3*q8
d2 = q3 + q6 + 2*q9
n - d3 - d4 = q2 + q3 + q4 + q6 + q8 + q9
```

For every one of the above solutions, there are several rearrangements of the digits, which can be found by the formula:

```
X = n! / ( q2! * q3! * ... q9! )
```

which have to be summed up.

There may be a closed formula for this, using generating functions, you could post it at Math.SE

Example for `p=24`

, `n=3`

:

```
p = 2^3 * 3^1 * 5^0 * 7^0
```

and we have:

```
d1=3, d2=1, d3=0, d4=0
```

The integer solutions to:

```
3 = q2 + 2*q4 + q6 + 3*q8
1 = q3 + q6 + 2*q9
3 = q2 + q3 + q4 + q6 + q8 + q9
```

are `(q2, q3, q4, q6, q8, q9) =`

:

```
(2, 0, 0, 1, 0, 0)
(1, 1, 1, 0, 0, 0)
```

which give:

```
3! / ( 2! * 1! ) = 3
3! / ( 1! * 1! * 1! ) = 6
```

and `3+6 = 9`

total solutions.

Example for `p=3628800`

, `n=10`

:

```
p = 2^8 * 3^4 * 5^1 * 7^1
```

and we have:

```
d1=8, d2=4, d3=1, d4=1
```

The integer solutions to:

```
8 = q2 + 2*q4 + q6 + 3*q8
4 = q3 + q6 + 2*q9
8 = q2 + q3 + q4 + q6 + q8 + q9
```

are `(q2, q3, q4, q6, q8, q9)`

(along with the corresponding digits and the rearrangements per solution):

```
(5, 0, 0, 0, 1, 2) 22222899 57 10! / (5! 2!) = 15120
(4, 0, 2, 0, 0, 2) 22224499 57 10! / (4! 2! 2!) = 37800
(4, 1, 0, 1, 1, 1) 22223689 57 10! / (4!) = 151200
(3, 2, 1, 0, 1, 1) 22233489 57 10! / (3! 2!) = 302400
(4, 0, 1, 2, 0, 1) 22224669 57 10! / (4! 2!) = 75600
(3, 1, 2, 1, 0, 1) 22234469 57 10! / (3! 2!) = 302400
(2, 2, 3, 0, 0, 1) 22334449 57 10! / (3! 2! 2!) = 151200
(2, 4, 0, 0, 2, 0) 22333388 57 10! / (4! 2! 2!) = 37800
(3, 2, 0, 2, 1, 0) 22233668 57 10! / (3! 2! 2!) = 151200
(2, 3, 1, 1, 1, 0) 22333468 57 10! / (3! 2!) = 302400
(1, 4, 2, 0, 1, 0) 23333448 57 10! / (4! 2!) = 75600
(4, 0, 0, 4, 0, 0) 22226666 57 10! / (4! 4!) = 6300
(3, 1, 1, 3, 0, 0) 22234666 57 10! / (3! 3!) = 100800
(2, 2, 2, 2, 0, 0) 22334466 57 10! / (2! 2! 2! 2!) = 226800
(1, 3, 3, 1, 0, 0) 23334446 57 10! / (3! 3!) = 100800
(0, 4, 4, 0, 0, 0) 33334444 57 10! / (4! 4!) = 6300
```

which is `2043720`

total solutions, if I haven't done any mistakes..

## comments: