Last active
February 7, 2016 15:20
-
-
Save joanromano/20f1732dd1dbb2dea5d7 to your computer and use it in GitHub Desktop.
Recursive greatest common divider
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
func greatestCommonDivisor(first: Int, second: Int) -> Int { | |
return internalPrivateGreatestCommonDivisor(first, second, min(first, second)) | |
} | |
private func internalPrivateGreatestCommonDivisor(first: Int, second: Int, divider: Int) -> Int { | |
if divider == 0 { | |
return 0 | |
} | |
if (first % divider == 0) && | |
(second % divider == 0) { | |
return divider | |
} | |
return internalPrivateGreatestCommonDivisor(first, second, divider-1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment