階乗進法 factorial representation

改訂新版 C言語による標準アルゴリズム事典をSwiftでコーディング

アルゴリズム

階乗進法 factorial representation

実行

Playground

コード

let N = 3

func factorial() {
    var c = [Int](repeating: 0, count: N + 2)
    var i = 0
    var k:Int
    repeat {
        print("\(i)", separator: "", terminator: "")
        i = i + 1
        k = N
        while k >= 1 {
            print(" \(c[k])", separator: "", terminator: "")
            k = k - 1
        }
        print()
        k = 1
        while c[k] == k {
            c[k] = 0
            k = k + 1
        }
        c[k] = c[k] + 1
    } while k <= N
}

factorial()

Algorithm,Swift

Posted by shi-n