Guest User

Factorial, the funny way

a guest
Jun 9th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.45 KB | None | 0 0
  1. ```
  2. # lets count the permutations of [1..n]
  3. def iterate(n):
  4.     co = 0
  5.     x = [0]
  6.     while len(x) > 0:
  7.         c = len(x) - 1
  8.         x[c] += 1
  9.  
  10.         if x[c] <= n:
  11.             if all(x[i] != x[c] for i in range(c)):
  12.                 if c < n - 1:
  13.                     x.append(0)
  14.                 else:
  15.                     co += 1
  16.                     # print(co, x)
  17.         else:
  18.             x.pop()
  19.     return co
  20.  
  21. print(iterate(5))
  22. ```
Advertisement
Add Comment
Please, Sign In to add comment