Skip to content

Commit 4ac7e54

Browse files
committed
Merge branch 'develop'
2 parents 4284c98 + 39e7036 commit 4ac7e54

5 files changed

+42
-31
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
[English](README.md) | [Korean](README_ko-KR.md)
88

9-
This repository is an implementation of algorithms, data structures, and problem-solving. These are written in C++, Python, Java, and Go, and each language uses the following test framework/package: [Google Test](https://google.github.io/googletest/)(C++), [pytest](https://docs.pytest.org/)(Python), [JUnit](https://junit.org/)(Java), [go test](https://pkg.go.dev/cmd/go)(Go). Run tests to perform methods/functions on the algorithmic logic.
9+
This repository is an implementation of algorithms, data structures, and problem-solving. These are written in C++, Python, Java, and Go, and each language uses the following test framework: [Google Test](https://google.github.io/googletest/)(C++), [pytest](https://docs.pytest.org/)(Python), [JUnit](https://junit.org/)(Java), [go test](https://pkg.go.dev/cmd/go)(Go). Run tests to perform methods/functions on the algorithmic logic.
1010

1111
## Project Environments
1212

@@ -873,7 +873,7 @@ Math.abs(-34.5), Math.ceil(2.17), Math.floor(3.14), Math.max(x, -3), Math.min(x,
873873
- Fast Fourier transform: Fast Fourier transform is a mathematical algorithm that finds the discrete Fourier transform of a set of real numbers.
874874
- Greatest common divisor (GCD), CLRS#31.2: [python](python-algorithm/algorithm/math), [java](java-algorithm/src/main/java/com/example/algorithm/math), [go](go-algorithm/pkg/math) | Find the greatest common divisor of two numbers.
875875
- Integer factorization: [c++](cpp-algorithm/src/math), [java](java-algorithm/src/main/java/com/example/algorithm/math) | Integer factorization is the process of determining which prime numbers divide a given positive integer.
876-
- Least common multiple (LCM): [python](python-algorithm/algorithm/math), [java](java-algorithm/src/main/java/com/example/algorithm/math) | Find the least common multiple of two numbers.
876+
- Least common multiple (LCM): [python](python-algorithm/algorithm/math), [java](java-algorithm/src/main/java/com/example/algorithm/math), [go](go-algorithm/pkg/math) | Find the least common multiple of two numbers.
877877
- Miller-Rabin primality test, CLRS#31.8: [c++](cpp-algorithm/src/math) | Miller-Rabin primality test is a mathematical algorithm that finds whether a given number is prime.
878878
- Permutation: [c++](cpp-algorithm/src/math)(`Permutation`) | Find the permutation of a set of items.
879879
- Permutation, EPI#5.10: [c++](cpp-algorithm/src/math)(`ApplyPermutationWithAdditionalSpace`, `ApplyPermutationBySwap`) | Permute the elements of an array
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,20 @@
11
package math
22

3-
type GCD interface {
4-
gcd(a int, b int) int
5-
gcdExtended(a int, b int) (int, int, int)
6-
}
7-
8-
type NumberTuple struct {
9-
a int // dividend
10-
b int // divisor
11-
}
12-
13-
// GCD finds the greatest common divisor of two numbers.
14-
func (n NumberTuple) gcd() int {
15-
for n.b != 0 {
16-
n.a, n.b = n.b, n.a%n.b
3+
// GCDEuclidean finds the greatest common divisor of two numbers with Euclidean algorithm.
4+
// The `a` is the dividend and `b` is the divisor.
5+
func GCDEuclidean(a, b int) int {
6+
for b != 0 {
7+
a, b = b, a%b
178
}
18-
return n.a
9+
return a
1910
}
2011

21-
// GCDExtended finds the greatest common divisor of two numbers with extended Euclidean algorithm.
22-
func (n NumberTuple) gcdExtended() (int, int, int) {
23-
if n.b == 0 {
24-
return n.a, 1, 0
12+
// GCDEuclideanExtended finds the greatest common divisor of two numbers with extended Euclidean algorithm.
13+
// The `a` is the dividend and `b` is the divisor.
14+
func GCDEuclideanExtended(a, b int) (int, int, int) {
15+
if b == 0 {
16+
return a, 1, 0
2517
}
26-
d, x, y := NumberTuple{n.b, n.a % n.b}.gcdExtended()
27-
return d, y, x - (n.a/n.b)*y
18+
d, x, y := GCDEuclideanExtended(b, a%b)
19+
return d, y, x - (a/b)*y
2820
}

go-algorithm/pkg/math/greatest_common_divisor_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ import (
55
"testing"
66
)
77

8-
func TestGCD(t *testing.T) {
9-
assert.Equal(t, 12, NumberTuple{24, 36}.gcd())
10-
assert.Equal(t, 1, NumberTuple{17, 22}.gcd())
11-
assert.Equal(t, 20, NumberTuple{120, 500}.gcd())
8+
func TestGCDEuclidean(t *testing.T) {
9+
assert.Equal(t, 12, GCDEuclidean(24, 36))
10+
assert.Equal(t, 1, GCDEuclidean(17, 22))
11+
assert.Equal(t, 20, GCDEuclidean(120, 500))
1212
}
1313

14-
func TestGCDExtended(t *testing.T) {
15-
d1, _, _ := NumberTuple{24, 36}.gcdExtended()
14+
func TestGCDEuclideanExtended(t *testing.T) {
15+
d1, _, _ := GCDEuclideanExtended(24, 36)
1616
assert.Equal(t, 12, d1)
17-
d2, _, _ := NumberTuple{17, 22}.gcdExtended()
17+
d2, _, _ := GCDEuclideanExtended(17, 22)
1818
assert.Equal(t, 1, d2)
19-
d3, _, _ := NumberTuple{120, 500}.gcdExtended()
19+
d3, _, _ := GCDEuclideanExtended(120, 500)
2020
assert.Equal(t, 20, d3)
2121
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package math
2+
3+
// LCM finds the least common multiple of two numbers.
4+
// The `a` and `b` are the numbers.
5+
func LCM(a, b int) int {
6+
return a * b / GCDEuclidean(a, b)
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package math
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestLCM(t *testing.T) {
9+
assert.Equal(t, 72, LCM(24, 36))
10+
assert.Equal(t, 374, LCM(17, 22))
11+
assert.Equal(t, 3000, LCM(120, 500))
12+
}

0 commit comments

Comments
 (0)