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,15 +1,8 @@
|
|||||||
package main
|
package numerical
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
func factorial(num int) int {
|
func factorial(num int) int {
|
||||||
if num == 0 {
|
if num == 0 {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
return num * factorial(num - 1)
|
return num * factorial(num-1)
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
num := 10
|
|
||||||
result := factorial(num)
|
|
||||||
fmt.Println(result)
|
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,9 @@
|
|||||||
package main
|
package numerical
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
//using recursion
|
//using recursion
|
||||||
func fibo(num int) int {
|
func fibo(num int) int {
|
||||||
if num <= 1 {
|
if num <= 1 {
|
||||||
return num
|
return num
|
||||||
}
|
}
|
||||||
return fibo(num -1) + fibo(num - 2)
|
return fibo(num-1) + fibo(num-2)
|
||||||
}
|
|
||||||
|
|
||||||
func main(){
|
|
||||||
num := 10
|
|
||||||
result := fibo(num)
|
|
||||||
fmt.Println(result)
|
|
||||||
}
|
}
|
||||||
|
@ -1,47 +1,48 @@
|
|||||||
package gcd
|
package numerical
|
||||||
|
|
||||||
func gcd(x uint, y uint) uint {
|
// GCD returns gcd of x and y
|
||||||
|
func GCD(x uint, y uint) uint {
|
||||||
|
|
||||||
var shift uint = 0
|
var shift uint = 0
|
||||||
|
|
||||||
if x == y {
|
if x == y {
|
||||||
return x
|
return x
|
||||||
}
|
}
|
||||||
|
|
||||||
if x == 0 {
|
if x == 0 {
|
||||||
return y
|
return y
|
||||||
}
|
}
|
||||||
|
|
||||||
if y == 0 {
|
if y == 0 {
|
||||||
return x
|
return x
|
||||||
}
|
}
|
||||||
|
|
||||||
for shift := 0; (x | y) & 1 == 0; shift++ {
|
for shift := 0; (x|y)&1 == 0; shift++ {
|
||||||
x = x >> 1
|
x = x >> 1
|
||||||
y = y >> 1
|
y = y >> 1
|
||||||
}
|
}
|
||||||
|
|
||||||
for ; (x & 1) == 0 ; {
|
for (x & 1) == 0 {
|
||||||
x = x >> 1
|
x = x >> 1
|
||||||
}
|
}
|
||||||
|
|
||||||
for ; y == 0 ; {
|
for y == 0 {
|
||||||
|
|
||||||
for ; (y & 1) == 0 ; {
|
for (y & 1) == 0 {
|
||||||
y = y >> 1
|
y = y >> 1
|
||||||
}
|
}
|
||||||
|
|
||||||
if x > y {
|
if x > y {
|
||||||
t := x
|
t := x
|
||||||
x = y
|
x = y
|
||||||
y = t
|
y = t
|
||||||
}
|
}
|
||||||
|
|
||||||
y = y - x
|
y = y - x
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
y = y << shift
|
y = y << shift
|
||||||
|
|
||||||
return y
|
return y
|
||||||
}
|
}
|
||||||
|
@ -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