I’m trying to efficiently list numbers between 1 and 100. However I have to get rid of numbers with same digits.
Example:
12 according to this rule is the same of 21
13 is 31
14 is 41
so the for loop it won’t go over the same numbers.
I’m thinking a few tricks such as getting all the numbers from 1 to 100 and then deleting the found permutations of current number.
The reason I’m asking this because in large limits like 100000 it will fail.
Another example: 124 is equal to 142,241,214,412,421
I’m trying to efficiently list numbers between 1 and 100. However I have to
Share
You can apply recursion. Prototype of this function is then like:
EDIT: for completion I present here my solution (i think it has better readbility than from Ben Voigt and ascending output order
and here is testing code
http://ideone.com/Xm8Mv
How this works?
It is one of classics in recursion. First there is stopping condition. And then there is main loop.
Main loop where goes from
start_from_digitbecause all generated digits will be in non decreasing order. For instance ifcurrent_numberis15it will callprint_digitswhithIn each call it will check if we reached end whit
num_of_remaining_digitsand if not will continue from digit that is pushed asstart_from_digit(2nd param) usingcurrent_number