Skip to content

Commit b88388c

Browse files
committed
Merge branch 'develop'
2 parents acb640f + b2784e5 commit b88388c

File tree

8 files changed

+86
-22
lines changed

8 files changed

+86
-22
lines changed

.github/workflows/go.yml

+6-1
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@ jobs:
2121
- name: Setup Go
2222
uses: actions/setup-go@v5
2323
with:
24+
# prettier-ignore
2425
go-version: '1.22'
2526

27+
- name: Download dependencies
28+
working-directory: ${{github.workspace}}/go-algorithm
29+
run: make module
30+
2631
- name: Test
2732
working-directory: ${{github.workspace}}/go-algorithm
28-
run: go test -v ./...
33+
run: make test

.github/workflows/gradle.yml

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ jobs:
2121
- name: Setup JDK
2222
uses: actions/setup-java@v4
2323
with:
24+
# prettier-ignore
2425
distribution: 'temurin'
2526
java-version: '21'
2627

.github/workflows/poetry.yml

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ jobs:
2121
- name: Setup Python
2222
uses: actions/setup-python@v5
2323
with:
24+
# prettier-ignore
2425
python-version: '3.12'
2526

2627
- name: Install dependencies

go-algorithm/Makefile

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
.PHONY: all
2+
all: download verify test
3+
4+
.PHONY: module
5+
module: download verify
6+
7+
.PHONY: fmt
8+
fmt:
9+
go fmt ./...
10+
11+
.PHONY: clean
12+
clean:
13+
go clean
14+
15+
.PHONY: download
16+
download:
17+
go mod download -x
18+
19+
.PHONY: verify
20+
verify:
21+
go mod verify
22+
23+
.PHONY: build
24+
build:
25+
go build ./...
26+
27+
.PHONY: test
28+
test:
29+
go test -v ./...

go-algorithm/internal/math/gcd.go

-9
This file was deleted.

go-algorithm/internal/math/gcd_test.go

-12
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package math
2+
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
17+
}
18+
return n.a
19+
}
20+
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
25+
}
26+
d, x, y := NumberTuple{n.b, n.a % n.b}.gcdExtended()
27+
return d, y, x - (n.a/n.b)*y
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package math
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
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())
12+
}
13+
14+
func TestGCDExtended(t *testing.T) {
15+
d1, _, _ := NumberTuple{24, 36}.gcdExtended()
16+
assert.Equal(t, 12, d1)
17+
d2, _, _ := NumberTuple{17, 22}.gcdExtended()
18+
assert.Equal(t, 1, d2)
19+
d3, _, _ := NumberTuple{120, 500}.gcdExtended()
20+
assert.Equal(t, 20, d3)
21+
}

0 commit comments

Comments
 (0)