Skip to content

Instantly share code, notes, and snippets.

@acegreen
Created September 20, 2016 22:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acegreen/d4733f4042be116da03a42a811ab6bfa to your computer and use it in GitHub Desktop.
Save acegreen/d4733f4042be116da03a42a811ab6bfa to your computer and use it in GitHub Desktop.
Factorial!
// 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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment