Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1310 from tanjunchen/improve-code
Browse files Browse the repository at this point in the history
enhancement: improve GCDSlice performance
  • Loading branch information
lowzj authored Apr 28, 2020
2 parents ca96df3 + e73614c commit 4eda7c3
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 4eda7c3

Please sign in to comment.