Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
pkg/algorithm/algorithm.go:improve algorithm
Browse files Browse the repository at this point in the history
Signed-off-by: tanjunchen <[email protected]>
  • Loading branch information
tanjunchen committed Apr 28, 2020
1 parent ca96df3 commit e73614c
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions pkg/algorithm/algorithm.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@ func GCDSlice(s []int) int {
if length == 1 {
return s[0]
}

return GCD(s[length-1], GCDSlice(s[:length-1]))
commonDivisor := s[0]
for i := 1; i < length; i++ {
if commonDivisor == 1 {
return commonDivisor
}
commonDivisor = GCD(commonDivisor, s[i])
}
return commonDivisor
}

// GCD returns the greatest common divisor of x and y.
Expand Down

0 comments on commit e73614c

Please sign in to comment.