implemented binpow and prime finding
parent
06a2cc352e
commit
d95522c19a
@ -0,0 +1,14 @@
|
|||||||
|
package numerical
|
||||||
|
|
||||||
|
// BinPow evaluates (base ^ deg) % rem
|
||||||
|
func BinPow(base int, deg int, rem int) int {
|
||||||
|
var res = 1
|
||||||
|
for deg > 0 {
|
||||||
|
if (deg & 1) > 0 {
|
||||||
|
res = int(int64(res) * int64(base) % int64(rem))
|
||||||
|
}
|
||||||
|
base = int((int64(base) * int64(base)) % int64(rem))
|
||||||
|
deg >>= 1
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
package numerical
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
// TestBinPow tests binpow function
|
||||||
|
func TestBinPow(t *testing.T) {
|
||||||
|
|
||||||
|
if BinPow(2, 10, 121323) != 1024 {
|
||||||
|
t.Error("[Error] BinPow(2, 10) is wrong")
|
||||||
|
}
|
||||||
|
|
||||||
|
if BinPow(1, 10, 121323) != 1 {
|
||||||
|
t.Error("[Error] BinPow(1, 10) is wrong")
|
||||||
|
}
|
||||||
|
|
||||||
|
if BinPow(0, 123123, 2) != 0 {
|
||||||
|
t.Error("[Error] BinPow(0, 123123) is wrong")
|
||||||
|
}
|
||||||
|
}
|
@ -1,18 +1,19 @@
|
|||||||
package gcd
|
package numerical
|
||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
func Test_gcd(t *testing.T) {
|
// TestGcd tests gcd
|
||||||
|
func TestGcd(t *testing.T) {
|
||||||
|
|
||||||
if gcd(100, 200) != 50 {
|
if GCD(100, 200) != 50 {
|
||||||
t.Error("[Error] gcd(100, 200) is wrong")
|
t.Error("[Error] GCD(100, 200) is wrong")
|
||||||
}
|
}
|
||||||
|
|
||||||
if gcd(4, 2) != 1 {
|
if GCD(4, 2) != 1 {
|
||||||
t.Error("[Error] gcd(4,2) is wrong")
|
t.Error("[Error] GCD(4,2) is wrong")
|
||||||
}
|
}
|
||||||
|
|
||||||
if gcd(6, 3) != 3 {
|
if GCD(6, 3) != 3 {
|
||||||
t.Error("[Error] gcd(6,3) is wrong")
|
t.Error("[Error] GCD(6,3) is wrong")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,26 @@
|
|||||||
|
package numerical
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func arrayEquals(a, b []int) bool {
|
||||||
|
if len(a) != len(b) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for i, v := range a {
|
||||||
|
if v != b[i] {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestPrimeFinder tests prime finding
|
||||||
|
func TestPrimeFinder(t *testing.T) {
|
||||||
|
|
||||||
|
if !arrayEquals(PrimesUpTo(10), []int{2, 3, 5, 7}) {
|
||||||
|
t.Error("[Error] PrimesUpTo(10) is wrong")
|
||||||
|
}
|
||||||
|
if len(PrimesUpTo(100)) != 25 {
|
||||||
|
t.Error("[Error] PrimesUpTo(100) is wrong")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue