Skip to content

Instantly share code, notes, and snippets.

@grantstephens
Created August 31, 2023 15:08
Show Gist options
  • Save grantstephens/c9db36a15a86010e3db5f9490ff58155 to your computer and use it in GitHub Desktop.
Save grantstephens/c9db36a15a86010e3db5f9490ff58155 to your computer and use it in GitHub Desktop.
Golang Greatest Common Denomenator
package main
import (
"fmt"
)
func greatestCommonDenomenator(x []int) int {
gcd := func(a, b int) int {
for b != 0 {
t := b
b = a % b
a = t
}
return a
}
r := gcd(x[0], x[1])
if len(x) > 3 {
for i := 2; i < len(x); i++ {
r = gcd(r, x[i])
}
}
return r
}
func main() {
fmt.Println(greatestCommonDenomenator([]int{40, 55, 60}))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment