Skip to content

Instantly share code, notes, and snippets.

@joanromano
Last active February 7, 2016 15:20
Show Gist options
  • Save joanromano/20f1732dd1dbb2dea5d7 to your computer and use it in GitHub Desktop.
Save joanromano/20f1732dd1dbb2dea5d7 to your computer and use it in GitHub Desktop.
Recursive greatest common divider
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