diff --git a/helper/README.md b/helper/README.md index 64e4eb2..0d691b3 100644 --- a/helper/README.md +++ b/helper/README.md @@ -47,12 +47,14 @@ The information provided on this project is strictly for informational purposes - [func Field\[T, S any\]\(c \<\-chan \*S, name string\) \(\<\-chan T, error\)](<#Field>) - [func Filter\[T any\]\(c \<\-chan T, p func\(T\) bool\) \<\-chan T](<#Filter>) - [func First\[T any\]\(c \<\-chan T, count int\) \<\-chan T](<#First>) +- [func Gcd\(values ...int\) int](<#Gcd>) - [func Head\[T Number\]\(c \<\-chan T, count int\) \<\-chan T](<#Head>) - [func IncrementBy\[T Number\]\(c \<\-chan T, i T\) \<\-chan T](<#IncrementBy>) - [func JSONToChan\[T any\]\(r io.Reader\) \<\-chan T](<#JSONToChan>) - [func KeepNegatives\[T Number\]\(c \<\-chan T\) \<\-chan T](<#KeepNegatives>) - [func KeepPositives\[T Number\]\(c \<\-chan T\) \<\-chan T](<#KeepPositives>) - [func Last\[T any\]\(c \<\-chan T, count int\) \<\-chan T](<#Last>) +- [func Lcm\(values ...int\) int](<#Lcm>) - [func Map\[F, T any\]\(c \<\-chan F, f func\(F\) T\) \<\-chan T](<#Map>) - [func MapWithPrevious\[F, T any\]\(c \<\-chan F, f func\(T, F\) T, previous T\) \<\-chan T](<#MapWithPrevious>) - [func Multiply\[T Number\]\(ac, bc \<\-chan T\) \<\-chan T](<#Multiply>) @@ -479,6 +481,15 @@ func First[T any](c <-chan T, count int) <-chan T First takes a channel of values and returns a new channel containing the first N values. + +## func [Gcd]() + +```go +func Gcd(values ...int) int +``` + +Gcd calculates the Greatest Common Divisor of the given numbers. + ## func [Head]() @@ -567,6 +578,15 @@ func Last[T any](c <-chan T, count int) <-chan T Last takes a channel of values and returns a new channel containing the last N values. + +## func [Lcm]() + +```go +func Lcm(values ...int) int +``` + +Lcm calculates the Least Common Multiple of the given numbers. + ## func [Map]() diff --git a/helper/gcd.go b/helper/gcd.go new file mode 100644 index 0000000..5f19a44 --- /dev/null +++ b/helper/gcd.go @@ -0,0 +1,24 @@ +// Copyright (c) 2021-2024 Onur Cinar. +// The source code is provided under GNU AGPLv3 License. +// https://github.com/cinar/indicator + +package helper + +// Gcd calculates the Greatest Common Divisor of the given numbers. +func Gcd(values ...int) int { + gcd := values[0] + + for i := 1; i < len(values); i++ { + value := values[i] + + for value > 0 { + gcd, value = value, gcd%value + } + + if gcd == 1 { + break + } + } + + return gcd +} diff --git a/helper/gcd_test.go b/helper/gcd_test.go new file mode 100644 index 0000000..4e978e7 --- /dev/null +++ b/helper/gcd_test.go @@ -0,0 +1,38 @@ +// Copyright (c) 2021-2024 Onur Cinar. +// The source code is provided under GNU AGPLv3 License. +// https://github.com/cinar/indicator + +package helper_test + +import ( + "testing" + + "github.com/cinar/indicator/v2/helper" +) + +func TestGcdWithTwoValues(t *testing.T) { + actual := helper.Gcd(1220, 512) + expected := 4 + + if actual != expected { + t.Fatalf("actual %d expected %d", actual, expected) + } +} + +func TestGcdWithThreeValues(t *testing.T) { + actual := helper.Gcd(1, 2, 5) + expected := 1 + + if actual != expected { + t.Fatalf("actual %d expected %d", actual, expected) + } +} + +func TestGcdWithFourValues(t *testing.T) { + actual := helper.Gcd(2, 4, 6, 12) + expected := 2 + + if actual != expected { + t.Fatalf("actual %d expected %d", actual, expected) + } +} diff --git a/helper/lcm.go b/helper/lcm.go new file mode 100644 index 0000000..636d448 --- /dev/null +++ b/helper/lcm.go @@ -0,0 +1,16 @@ +// Copyright (c) 2021-2024 Onur Cinar. +// The source code is provided under GNU AGPLv3 License. +// https://github.com/cinar/indicator + +package helper + +// Lcm calculates the Least Common Multiple of the given numbers. +func Lcm(values ...int) int { + lcm := values[0] + + for i := 1; i < len(values); i++ { + lcm = (values[i] * lcm) / Gcd(values[i], lcm) + } + + return lcm +} diff --git a/helper/lcm_test.go b/helper/lcm_test.go new file mode 100644 index 0000000..5529c7a --- /dev/null +++ b/helper/lcm_test.go @@ -0,0 +1,38 @@ +// Copyright (c) 2021-2024 Onur Cinar. +// The source code is provided under GNU AGPLv3 License. +// https://github.com/cinar/indicator + +package helper_test + +import ( + "testing" + + "github.com/cinar/indicator/v2/helper" +) + +func TestLcmWithTwoValues(t *testing.T) { + actual := helper.Lcm(18, 32) + expected := 288 + + if actual != expected { + t.Fatalf("actual %d expected %d", actual, expected) + } +} + +func TestLcmWithFourValues(t *testing.T) { + actual := helper.Lcm(1, 2, 8, 6) + expected := 24 + + if actual != expected { + t.Fatalf("actual %d expected %d", actual, expected) + } +} + +func TestLcmWithFiveValues(t *testing.T) { + actual := helper.Lcm(2, 7, 3, 9, 8) + expected := 504 + + if actual != expected { + t.Fatalf("actual %d expected %d", actual, expected) + } +}