Skip to content

Instantly share code, notes, and snippets.

@chefnobody
Created March 18, 2018 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chefnobody/cde788cd419f8b4cdf0139cc59bf3e0e to your computer and use it in GitHub Desktop.
Save chefnobody/cde788cd419f8b4cdf0139cc59bf3e0e to your computer and use it in GitHub Desktop.
Calculates the greatest common denominator of two numbers
import Foundation
func gcd(_ a:Int, _ b:Int) -> Int {
let r = a % b
if r != 0 {
return gcd(b, r)
} else {
return b
}
}
gcd(52, 39)
gcd(228, 36)
gcd(51357, 3819)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment