find the greatest common divisor of a list of numbers in go

Here's an implementation in Go that uses Euclid's algorithm to find the greatest common divisor of a list of numbers.

main.go
func GCD(nums []int) int {
    if len(nums) == 0 {
        return 0
    }
    result := nums[0]
    for i := 1; i < len(nums); i++ {
        result = gcd(result, nums[i])
    }
    return result
}

func gcd(a, b int) int {
    if b == 0 {
        return a
    }
    return gcd(b, a%b)
}
287 chars
18 lines

The GCD function takes in a list of integers and returns their greatest common divisor using Euclid's algorithm. It first initializes the result to the first number in the list, then iterates through the rest of the numbers, updating the result using the gcd function. The gcd function recursively calculates the greatest common divisor of two numbers using Euclid's algorithm.

gistlibby LogSnag