Greatest Common Divisor from a set of more than 2 integers

后端 未结 13 980
遇见更好的自我
遇见更好的自我 2020-12-09 04:25

There are several questions on Stack Overflow discussing how to find the Greatest Common Divisor of two values. One good answer shows a neat recursive function

13条回答
  •  醉梦人生
    2020-12-09 05:16

    let a = 3
    let b = 9
    
    func gcd(a:Int, b:Int) -> Int {
        if a == b {
            return a
        }
        else {
            if a > b {
                return gcd(a:a-b,b:b)
            }
            else {
                return gcd(a:a,b:b-a)
            }
        }
    }
    print(gcd(a:a, b:b))
    

提交回复
热议问题