 
 
 /*
-  Calculates P(n, k), the number of permutations of n distinct symbols
-  in groups of size k.
-*/
+ Calculates P(n, k), the number of permutations of n distinct symbols
+ in groups of size k.
+ */
 func permutations(n: Int, _ k: Int) -> Int {
-  var n = n
-  var answer = n
-  for _ in 1..<k {
-    n -= 1
-    answer *= n
-  }
-  return answer
+    var n = n
+    var answer = n
+    for _ in 1..<k {
+        n -= 1
+        answer *= n
+    }
+    return answer
 }
 
 permutations(5, 3)



/*
  Prints out all the permutations of an n-element collection.

  The initial array must be initialized with all zeros. The algorithm
  uses 0 as a flag that indicates more work to be done on each level
  of the recursion.

  Original algorithm by Robert Sedgewick.
  See also Dr.Dobb's Magazine June 1993, Algorithm Alley
*/
func permuteSedgewick(a: [Int], _ n: Int, inout _ pos: Int) {
  var a = a
  pos += 1
  a[n] = pos
  if pos == a.count - 1 {
    print(a)              // display the current permutation
  } else {
    for i in 0..<a.count {
      if a[i] == 0 {
        permuteSedgewick(a, i, &pos)
      }
    }
  }
  pos -= 1
  a[n] = 0
}

print("\nSedgewick permutations:")


/* Calculates n! */
func factorial(n: Int) -> Int {
    var n = n
    var result = 1
    while n > 1 {
        result *= n
        n -= 1
    }
    return result
}

factorial(5)
