4
$\begingroup$

I tried to search this but could not find. Given a number of digits(the maximum being 0 to 9), how can I generate a list of all possible numbers without repeating the digits in each number? e.g. Given the digits 1,2 & 3 I should get 123, 132, 213. 231, 312, & 321. I want to generate such a list of digits with all digits from 0 to 9. Thanks. I am relatively new at Mathematica and am trying to learn.

$\endgroup$

1 Answer 1

6
$\begingroup$

The lists of digits of the numbers you are looking for are mere permutations of the digits $0,\,1,\,2\,\dotsc,9$. Permutations can provide you with these permutations; FromDigits turns them into numbers.

FromDigits /@ Permutations[Range[0,9]]

This simply ignores leading 0-digits. If you want only numbers with 10 true digits, you can use the following:

FromDigits /@ Drop[Permutations[Range[0, 9]], 9!]
$\endgroup$
4
  • $\begingroup$ 0 can't be the first digit. $\endgroup$ Commented Jul 13, 2018 at 6:46
  • $\begingroup$ Leading 0 digits are removed automatically. $\endgroup$ Commented Jul 13, 2018 at 6:58
  • 2
    $\begingroup$ Or this Drop[FromDigits /@ Permutations[Range[0, 3]], 9!] $\endgroup$ Commented Jul 14, 2018 at 4:55
  • $\begingroup$ @OkkesDulgerci Very good point. It's much faster. Thanks. $\endgroup$ Commented Jul 15, 2018 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.