Last active
August 29, 2015 14:11
-
-
Save hooman/f9af191bfc9308e63ab4 to your computer and use it in GitHub Desktop.
Another one of my Swift samples for RosettaCode.org
This file contains 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
// Recursive: | |
func factorial(num: Int) -> Int { | |
return num < 2 ? 1 : num * factorial(num - 1) | |
} | |
// Iterative: | |
func factorial2(num: Int) -> Int { | |
return num < 2 ? 1 : reduce(1...num, 1, *) | |
} | |
factorial2(5) | |
factorial2(5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment