/** * Beispiel aus * * - Algorithmen und Datenstrukturen für Dummies * - von Andreas Gogol-Döring und Thomas Letschert * - Verlag Wiley-VCH; Oktober 2019 * - Kapitel 3, Daten und ihre Struktur * * @author A. Gogol-Döring, Th. Letschert */ import Foundation struct AuD_03_07_FIter { static func FItera(n: Int) -> Int { var acc1: [Int] = Array() var nV = n while (nV != 0) { acc1.insert(nV, at:0) nV = nV - 1 } var acc2: Int = 1 var rev: ArraySlice = acc1[0 ... acc1.count-1] while (rev.count > 0) { let head = rev[rev.startIndex] acc2 = acc2 * head let rest = rev.suffix(from: rev.startIndex+1) rev = rest } return acc2 } static func FTR(n: Int) -> Int { func f(n: Int, acc: Int) -> Int { if (n == 0) { return acc } else { return f(n: n-1, acc: acc * n) } } return f(n: n, acc: 1) } static func FIter(n: Int) -> Int { var f = 1 var nV = n while (nV != 0) { let (n_, f_) = (nV-1, nV * f) nV = n_ f = f_ } return f } static func run() { print(FItera(n: 10)) print(FTR(n: 10)) print(FIter(n: 10)) } }