Guest User

Untitled

a guest
Jan 23rd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.85 KB | None | 0 0
  1. > function permgen (a, n)
  2. >>       if n == 0 then
  3. >>         printResult(a)
  4. >>       else
  5. >>         for i=1,n do
  6. >>    
  7. >>           -- put i-th element as the last one
  8. >>           a[n], a[i] = a[i], a[n]
  9. >>    
  10. >>           -- generate all permutations of the other elements
  11. >>           permgen(a, n - 1)
  12. >>    
  13. >>           -- restore i-th element
  14. >>           a[n], a[i] = a[i], a[n]
  15. >>    
  16. >>         end
  17. >>       end
  18. >>     end
  19. > function printResult (a)
  20. >>       for i,v in ipairs(a) do
  21. >>         io.write(v, " ")
  22. >>       end
  23. >>       io.write("\n")
  24. >>     end
  25. > permgen ({1,2,3,4}, 4)
  26. 2 3 4 1
  27. 3 2 4 1
  28. 3 4 2 1
  29. 4 3 2 1
  30. 2 4 3 1
  31. 4 2 3 1
  32. 4 3 1 2
  33. 3 4 1 2
  34. 3 1 4 2
  35. 1 3 4 2
  36. 4 1 3 2
  37. 1 4 3 2
  38. 2 4 1 3
  39. 4 2 1 3
  40. 4 1 2 3
  41. 1 4 2 3
  42. 2 1 4 3
  43. 1 2 4 3
  44. 2 3 1 4
  45. 3 2 1 4
  46. 3 1 2 4
  47. 1 3 2 4
  48. 2 1 3 4
  49. 1 2 3 4
  50. >
Add Comment
Please, Sign In to add comment