Optimize the solution

Check only unique combinations of digits and calculate permutations
Optimize the solution

Iterate only through the second half of the numbers in the range