Consider number 194 declared as type int
Is it possible to obtain it’s digits permutations like other ints efficiently?
Number: 194
419 int
491 int
914 int
941 int
I am using the next_permutation however it only works with arrays. So I thought it wouldn’t be wise to convert int to an int array (?!) then obtain the permutation as an array and convert it to it.
Any suggestions?
Permuting the digits is basically a string-operation, not a (simple) mathematical operation. Converting to an array (string) and then using
next_permutation()sounds more sensible than trying to do it mathematically.Here’s the mathematical version – without intermediate values saved:
With intermediate values, it’s a little easier to see what’s going on (except that I generated different assignments for
bthroughfthis time).Use the
next_permutation()mechanism; it will generalize to 4-digit and 5-digit and N-digit numbers where this will not.