Skip to content

Commit b9e84b5

Browse files
committed
Files refactored for lint compliance. Tests and benchmarks added.
Signed-off-by: Jobin John <[email protected]>
1 parent 6b860fc commit b9e84b5

File tree

11 files changed

+217
-117
lines changed

11 files changed

+217
-117
lines changed

.github/workflows/golangci-lint.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,7 @@ jobs:
3434
# - run: golangci-lint run --no-config "strings/single string matching" || true
3535
- run: golangci-lint run --no-config strings/levenshteindistance
3636
- run: golangci-lint run --no-config strings/naivesearch
37+
- run: golangci-lint run --no-config math/gcd
38+
- run: golangci-lint run --no-config math/power
39+
- run: golangci-lint run --no-config math/primecheck
40+
- run: golangci-lint run --no-config math/sieve || true"

math/fastExponent.go

Lines changed: 0 additions & 49 deletions
This file was deleted.

math/gcd/greatest_common_divisor.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package gcd
2+
3+
// Gcd finds and returns the greatest common divisor of a given integer.
4+
func Gcd(a, b int) int {
5+
if b == 0 {
6+
return a
7+
}
8+
return Gcd(b, a%b)
9+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package gcd
2+
3+
import "testing"
4+
5+
var testCases = []struct {
6+
name string
7+
a int
8+
b int
9+
output int
10+
}{
11+
{"gcd of 10 and 0", 10, 0, 10},
12+
{"gcd of 98 and 56", 98, 56, 14},
13+
{"gcd of 0 and 10", 0, 10, 10},
14+
}
15+
16+
func TestGCD(t *testing.T) {
17+
for _, tc := range testCases {
18+
t.Run(tc.name, func(t *testing.T) {
19+
actual := Gcd(tc.a, tc.b)
20+
if actual != tc.output {
21+
t.Errorf("Expected GCD of %d and %d to be: %v, but got: %d", tc.a, tc.b, tc.output, actual)
22+
}
23+
})
24+
}
25+
}
26+
27+
func BenchmarkGCD(b *testing.B) {
28+
for i := 0; i < b.N; i++ {
29+
Gcd(98, 56)
30+
}
31+
}

math/greatest_common_divisor.go

Lines changed: 0 additions & 17 deletions
This file was deleted.

math/power/fastExponent.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package power
2+
3+
// IterativePower is iterative O(logn) function for pow(x, y)
4+
func IterativePower(n uint, power uint) uint {
5+
var res uint = 1
6+
for power > 0 {
7+
8+
if (power & 1) != 0 {
9+
res = res * n
10+
}
11+
12+
power = power >> 1
13+
n = n * n
14+
}
15+
return res
16+
}
17+
18+
// RecursivePower is recursive O(logn) function for pow(x, y)
19+
func RecursivePower(n uint, power uint) uint {
20+
if power == 0 {
21+
return 1
22+
}
23+
var temp = RecursivePower(n, power/2)
24+
if power%2 == 0 {
25+
return temp * temp
26+
}
27+
return n * temp * temp
28+
}
29+
30+
// RecursivePower1 is recursive O(n) function for pow(x, y)
31+
func RecursivePower1(n uint, power uint) uint {
32+
if power == 0 {
33+
return 1
34+
} else if power%2 == 0 {
35+
return RecursivePower1(n, power/2) * RecursivePower1(n, power/2)
36+
} else {
37+
return n * RecursivePower1(n, power/2) * RecursivePower1(n, power/2)
38+
}
39+
}

math/power/fastExponent_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package power
2+
3+
import "testing"
4+
5+
var testCases = []struct {
6+
name string
7+
base uint
8+
power uint
9+
expected uint
10+
}{
11+
{"0^2", 0, 2, 0},
12+
{"2^0", 2, 0, 1},
13+
{"2^3", 2, 3, 8},
14+
{"8^3", 8, 3, 512},
15+
{"10^5", 10, 5, 100000},
16+
}
17+
18+
func TestIterativePower(t *testing.T) {
19+
for _, tc := range testCases {
20+
t.Run(tc.name, func(t *testing.T) {
21+
actual := IterativePower(tc.base, tc.power)
22+
if actual != tc.expected {
23+
t.Errorf("Expected %d to the power of %d to be: %d, but got: %d", tc.base, tc.power, tc.expected, actual)
24+
}
25+
})
26+
}
27+
}
28+
29+
func TestRecursivePower(t *testing.T) {
30+
for _, tc := range testCases {
31+
t.Run(tc.name, func(t *testing.T) {
32+
actual := RecursivePower(tc.base, tc.power)
33+
if actual != tc.expected {
34+
t.Errorf("Expected %d to the power of %d to be: %d, but got: %d", tc.base, tc.power, tc.expected, actual)
35+
}
36+
})
37+
}
38+
}
39+
40+
func TestRecursivePower1(t *testing.T) {
41+
for _, tc := range testCases {
42+
t.Run(tc.name, func(t *testing.T) {
43+
actual := RecursivePower1(tc.base, tc.power)
44+
if actual != tc.expected {
45+
t.Errorf("Expected %d to the power of %d to be: %d, but got: %d", tc.base, tc.power, tc.expected, actual)
46+
}
47+
})
48+
}
49+
}
50+
51+
func BenchmarkIterativePower(b *testing.B) {
52+
for i := 0; i < b.N; i++ {
53+
IterativePower(10, 5)
54+
}
55+
}
56+
57+
func BenchmarkRecursivePower(b *testing.B) {
58+
for i := 0; i < b.N; i++ {
59+
RecursivePower(10, 5)
60+
}
61+
}
62+
63+
func BenchmarkRecursivePower1(b *testing.B) {
64+
for i := 0; i < b.N; i++ {
65+
RecursivePower1(10, 5)
66+
}
67+
}

math/prime_check/prime_check_test.go

Lines changed: 0 additions & 44 deletions
This file was deleted.

math/prime_check/prime_check.go renamed to math/primecheck/prime_check.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
//A primality test is an algorithm for determining whether an input number is prime.Among other fields of mathematics, it is used for cryptography.
1+
package primecheck
2+
3+
// A primality test is an algorithm for determining whether an input number is prime.Among other fields of mathematics, it is used for cryptography.
24
//Unlike integer factorization, primality tests do not generally give prime factors, only stating whether the input number is prime or not.
35
//Source - Wikipedia https://en.wikipedia.org/wiki/Primality_test
4-
package main
56

7+
// NaiveApproach checks if an integer is prime or not. Returns a bool.
68
func NaiveApproach(n int) bool {
79
if n < 2 {
810
return false
@@ -16,6 +18,7 @@ func NaiveApproach(n int) bool {
1618
return true
1719
}
1820

21+
// PairApproach checks primality of an integer and returns a bool. More efficient than the naive approach as number of iterations are less.
1922
func PairApproach(n int) bool {
2023
if n < 2 {
2124
return false
@@ -27,8 +30,3 @@ func PairApproach(n int) bool {
2730
}
2831
return true
2932
}
30-
31-
//goos: windows
32-
//goarch: amd64
33-
//BenchmarkNaiveApproach-12 660980034 1.81 ns/op
34-
//BenchmarkPairApproach-12 664641356 1.82 ns/op

math/primecheck/prime_check_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package primecheck
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestTableNaiveApproach(t *testing.T) {
8+
var tests = []struct {
9+
name string
10+
input int
11+
expected bool
12+
}{
13+
{"smallest prime", 2, true},
14+
{"random prime", 3, true},
15+
{"neither prime nor composite", 1, false},
16+
{"random non-prime", 10, false},
17+
{"another random prime", 23, true},
18+
}
19+
20+
for _, test := range tests {
21+
t.Run(test.name, func(t *testing.T) {
22+
if output := NaiveApproach(test.input); output != test.expected {
23+
t.Errorf("For input: %d, expected: %v, but got: %v", test.input, test.expected, output)
24+
}
25+
})
26+
}
27+
28+
}
29+
func TestTablePairApproach(t *testing.T) {
30+
var tests = []struct {
31+
name string
32+
input int
33+
expected bool
34+
}{
35+
{"smallest prime", 2, true},
36+
{"random prime", 3, true},
37+
{"neither prime nor composite", 1, false},
38+
{"random non-prime", 10, false},
39+
{"another random prime", 23, true},
40+
}
41+
42+
for _, test := range tests {
43+
t.Run(test.name, func(t *testing.T) {
44+
if output := NaiveApproach(test.input); output != test.expected {
45+
t.Errorf("For input: %d, expected: %v, but got: %v", test.input, test.expected, output)
46+
}
47+
})
48+
}
49+
50+
}
51+
52+
func BenchmarkNaiveApproach(b *testing.B) {
53+
for i := 0; i < b.N; i++ {
54+
NaiveApproach(23)
55+
}
56+
}
57+
58+
func BenchmarkPairApproach(b *testing.B) {
59+
for i := 0; i < b.N; i++ {
60+
PairApproach(23)
61+
}
62+
}

0 commit comments

Comments
 (0)