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 <tanjunchen20@gmail.com>
  • Loading branch information
tanjunchen authored and lowzj committed Apr 28, 2020
1 parent 0ba788d commit 5ff4530
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 5ff4530

Please sign in to comment.