Created
August 31, 2023 15:08
-
-
Save grantstephens/c9db36a15a86010e3db5f9490ff58155 to your computer and use it in GitHub Desktop.
Golang Greatest Common Denomenator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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