-6

Many of my friends and teachers argued me that the program of finding all possible permutations of digits, in a number of 'n' digits without using recursion is not possible while one said it is possible but tricky. So I need some help solving this problem. Thank You.

  • Check http://stackoverflow.com/questions/15848887/calculate-all-unique-permutations-using-nested-loops-in-python – mc20 Feb 03 '17 at 09:24
  • [Permutation](https://en.wikipedia.org/wiki/Permutation) is about set with multiple elements ... "permutations of a (one) number" doesn't make sens ... so what do you mean by "permutations of a number" – Selvin Feb 03 '17 at 09:24
  • @Selvin no, natural numbers can be defined using sets :) https://en.wikipedia.org/wiki/Set-theoretic_definition_of_natural_numbers – Kojotak Feb 03 '17 at 09:26
  • 2
    Everything that can be done with recursion can be done with loops and stacks as well - in fact that's what recursion basically does: put the data on the stack and do another "iteration" by calling the method again. – Thomas Feb 03 '17 at 09:27
  • *natural **numbers** can be defined using sets* ... he wrote "permutations of a number" ... while set can may one element it has only one permutation then ... maybe he was talking about permutation of digits in number? – Selvin Feb 03 '17 at 09:28

1 Answers1

0

Recursion is the act of a function calling itself. Under the hood function calls are pushed to the stack of the system and when a function finishes execution, it will be popped from the stack. A stack is a data structure which stores items and has the following operations:

  • push: puts an item to the end of a stack
  • pop: removes an item from the end of a stack and returns it
  • top: returns the item at the end of a stack

You need to define what your stack will store. In our case that could be the current, possibly unfinished permutation. If you want an iterative solution, your job is to handle the stack yourself.

Lajos Arpad
  • 45,912
  • 26
  • 82
  • 148
  • I cannot use recursion or stack, only simple looping statement. – darker_speck Feb 04 '17 at 17:19
  • @darker_speck I did not suggest that you should use recursion. I was talking about the non-recursive implementation. And for that purpose you need a stack, where you store the current status of your operation. Without a stack you cannot resolve this problem. – Lajos Arpad Feb 04 '17 at 18:09