Swift - Write program to compute the GCD (greatest common divisor) of two integers

Requirements

Write program to compute the GCD (greatest common divisor) of two integers

Hint

In mathematics, GCD (greatest common divisor) of two or more integers is that largest positive number that can divide the numbers without a remainder.

For example, the GCD of 8 and 12 is 4.

Demo

func GCD(var a: Int, var b: Int) -> Int
{
    var remainder = 0
    while( b != 0 ) {
        remainder = a % b//w  w w.j  av a 2 s  . co  m
        a = b
        b = remainder
    }
    return a
}

print(GCD(12,b:8))  //4---