Created
September 20, 2016 22:55
-
-
Save acegreen/d4733f4042be116da03a42a811ab6bfa to your computer and use it in GitHub Desktop.
Factorial!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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