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$
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$
  • $\begingroup$ 0 can't be the first digit. $\endgroup$ – OkkesDulgerci Jul 13 '18 at 6:46
  • $\begingroup$ Leading 0 digits are removed automatically. $\endgroup$ – Henrik Schumacher Jul 13 '18 at 6:58
  • 2
    $\begingroup$ Or this Drop[FromDigits /@ Permutations[Range[0, 3]], 9!] $\endgroup$ – OkkesDulgerci Jul 14 '18 at 4:55
  • $\begingroup$ @OkkesDulgerci Very good point. It's much faster. Thanks. $\endgroup$ – Henrik Schumacher Jul 15 '18 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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