/* * File: comb.kt * Purpose: Prints all k-subsets of the first n nonnegative integers * Compilation: kotlinc comb.kt * Execution: kotlin CombKt n k * where n, k are positive integers (0 <= k <= n) * Author: San Skulrattanakulchai */ fun main(args: Array) { val n = args[0].toInt() val k = args[1].toInt() val b = BooleanArray(n) fun comb(start: Int, i: Int) { if (i == k+1) { for (j in b.indices) if (b[j]) print(" $j") println() } else for (j in start until (n-k+i)) { b[j] = true comb(j+1, i+1) b[j] = false } } comb(0, 1) }