updated util packages

This commit is contained in:
alexchao26
2020-12-10 01:04:51 -05:00
parent b9a74cf122
commit 1fec4f74d3
47 changed files with 285 additions and 95 deletions
+25
View File
@@ -0,0 +1,25 @@
package mathutil
import (
"math"
)
func PythagoreanDistance(x1, y1, x2, y2 int) float64 {
xDiff := float64(x1 - x2)
yDiff := float64(y1 - y2)
sumOfSquares := math.Pow(xDiff, 2) + math.Pow(yDiff, 2)
return math.Sqrt(sumOfSquares)
}
func ManhattanDistance(x1, y1, x2, y2 int) int {
xDiff := x1 - x2
yDiff := y1 - y2
if xDiff < 0 {
xDiff *= -1
}
if yDiff < 0 {
yDiff *= -1
}
return xDiff + yDiff
}
+37
View File
@@ -0,0 +1,37 @@
package mathutil
import (
"fmt"
"strconv"
)
func MaxInt(nums ...int) int {
maxNum := nums[0]
for _, v := range nums {
if v > maxNum {
maxNum = v
}
}
return maxNum
}
func MinInt(nums ...int) int {
minNum := nums[0]
for _, v := range nums {
if v < minNum {
minNum = v
}
}
return minNum
}
func StrToInt(in string) int {
num, err := strconv.Atoi(in)
if err != nil {
panic(fmt.Sprintf("converting string to number: %s", err))
}
return num
}
func IntToStr(in int) string {
return strconv.Itoa(in)
}
+34
View File
@@ -0,0 +1,34 @@
package mathutil
import (
"math"
)
// GeneratePrimes returns the n-th prime number
// its param primes []int is intended to contain previously found prime numbers
// to reduce duplicated work, but still be testable
func GeneratePrimes(primes []int, n int) int {
if len(primes) < 2 {
primes = []int{2, 3}
}
if len(primes) >= n {
return primes[n-1]
}
for i := primes[len(primes)-1] + 2; len(primes) <= n; i += 2 {
// check if i is a prime number by checking if it is divisible by any of the previous values of primes
// stop at the square root of i
for _, v := range primes {
// not a prime, stop this loop
if i%v == 0 {
break
}
if math.Sqrt(float64(i)) < float64(v) {
// add to primes
primes = append(primes, i)
break
}
}
}
return primes[n-1]
}
+52
View File
@@ -0,0 +1,52 @@
package mathutil
import (
"fmt"
"testing"
)
func TestGeneratePrimes(t *testing.T) {
tests := []struct {
previousPrimes []int
n, expected int
}{
{[]int{2, 3}, 1, 2},
{[]int{2, 3}, 2, 3},
{[]int{2, 3}, 3, 5},
{[]int{2, 3}, 4, 7},
{[]int{2, 3, 5, 7}, 4, 7},
{[]int{2, 3, 5, 7}, 5, 11},
{[]int{2, 3, 5, 7}, 6, 13},
{[]int{2, 3}, 7, 17},
{[]int{2, 3}, 8, 19},
}
for _, test := range tests {
t.Run(fmt.Sprintf("%v-th prime number", test.n),
func(t *testing.T) {
ans := GeneratePrimes(test.previousPrimes, test.n)
if ans != test.expected {
t.Errorf("Expected %v-th prime to be %v, got %v", test.n, test.expected, ans)
}
},
)
}
}
// run go test -bench=. from within this util folder
// Benchmark
func benchGenPrimes(n int, b *testing.B) {
for i := 0; i < b.N; i++ {
GeneratePrimes([]int{2, 3}, n)
}
}
// Benchmark generating different magnitudes of primes...
func BenchmarkGeneratePrimes10(b *testing.B) { benchGenPrimes(10, b) }
func BenchmarkGeneratePrimes100(b *testing.B) { benchGenPrimes(100, b) }
func BenchmarkGeneratePrimes1000(b *testing.B) { benchGenPrimes(1000, b) }
func BenchmarkGeneratePrimes10000(b *testing.B) { benchGenPrimes(10000, b) }
func BenchmarkGeneratePrimes100000(b *testing.B) { benchGenPrimes(100000, b) }
func BenchmarkGeneratePrimes1000000(b *testing.B) { benchGenPrimes(1000000, b) }
// takes ~2 minutes on my mac
// func BenchmarkGeneratePrimes10000000(b *testing.B) { benchGenPrimes(10000000, b) }