Skip to content

Instantly share code, notes, and snippets.

@acegreen
Created September 20, 2016 22:55
Show Gist options
  • Select an option

  • Save acegreen/d4733f4042be116da03a42a811ab6bfa to your computer and use it in GitHub Desktop.

Select an option

Save acegreen/d4733f4042be116da03a42a811ab6bfa to your computer and use it in GitHub Desktop.

Revisions

  1. acegreen created this gist Sep 20, 2016.
    24 changes: 24 additions & 0 deletions Factorial!
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    // Mark: - Factorial!

    // Iterative
    func factorialIterative(of number: Int) -> Int {

    var factorial: Int = 1

    for i in 2...number {
    factorial *= i
    }

    return factorial
    }

    print("factorial Iterative", factorialIterative(of: 5))

    // Recursive
    func factorialRecursive(of number: Int) -> Int {
    guard number > 1 else { return 1 }

    return number * factorialRecursive(of: number - 1)
    }

    print("factorial recursive", factorialRecursive(of: 5))