-
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Describe Request Greatest Common Divisor (GCD) and Least Common Multiple (LCM) functions are added. # Change Type New feature.
- Loading branch information
Showing
5 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |