Skip to content

Commit

Permalink
GCD and LCD added. (#214)
Browse files Browse the repository at this point in the history
# Describe Request

Greatest Common Divisor (GCD) and Least Common Multiple (LCM) functions
are added.

# Change Type

New feature.
  • Loading branch information
cinar authored Sep 12, 2024
1 parent 4c51922 commit d38d1d9
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 0 deletions.
20 changes: 20 additions & 0 deletions helper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>)
Expand Down Expand Up @@ -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.

<a name="Gcd"></a>
## func [Gcd](<https://github.com/cinar/indicator/blob/master/helper/gcd.go#L8>)

```go
func Gcd(values ...int) int
```

Gcd calculates the Greatest Common Divisor of the given numbers.

<a name="Head"></a>
## func [Head](<https://github.com/cinar/indicator/blob/master/helper/head.go#L16>)

Expand Down Expand Up @@ -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.

<a name="Lcm"></a>
## func [Lcm](<https://github.com/cinar/indicator/blob/master/helper/lcm.go#L8>)

```go
func Lcm(values ...int) int
```

Lcm calculates the Least Common Multiple of the given numbers.

<a name="Map"></a>
## func [Map](<https://github.com/cinar/indicator/blob/master/helper/map.go#L17>)

Expand Down
24 changes: 24 additions & 0 deletions helper/gcd.go
Original file line number Diff line number Diff line change
@@ -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
}
38 changes: 38 additions & 0 deletions helper/gcd_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
16 changes: 16 additions & 0 deletions helper/lcm.go
Original file line number Diff line number Diff line change
@@ -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
}
38 changes: 38 additions & 0 deletions helper/lcm_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit d38d1d9

Please sign in to comment.