1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-08 22:52:29 +08:00

doc: add document for GCD and LCM function

This commit is contained in:
dudaodong
2023-05-06 11:28:38 +08:00
parent 654ba15aaf
commit e2aeb8ec07
5 changed files with 210 additions and 8 deletions

View File

@@ -287,8 +287,8 @@ func gcd[T constraints.Integer](a, b T) T {
func LCM[T constraints.Integer](integers ...T) T {
result := integers[0]
for i := 1; i < len(integers)-1; i++ {
result = lcm(result, integers[i])
for k := range integers {
result = lcm(integers[k], result)
}
return result
@@ -296,5 +296,8 @@ func LCM[T constraints.Integer](integers ...T) T {
// find Least Common Multiple (LCM) via GCD.
func lcm[T constraints.Integer](a, b T) T {
if a == 0 || b == 0 {
panic("lcm function: provide non zero integers only.")
}
return a * b / gcd(a, b)
}