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
|
||||
import "fmt"
|
||||
package numerical
|
||||
|
||||
func factorial(num int) int {
|
||||
if num == 0 {
|
||||
return 1
|
||||
}
|
||||
return num * factorial(num - 1)
|
||||
}
|
||||
|
||||
func main() {
|
||||
num := 10
|
||||
result := factorial(num)
|
||||
fmt.Println(result)
|
||||
if num == 0 {
|
||||
return 1
|
||||
}
|
||||
return num * factorial(num-1)
|
||||
}
|
||||
|
@ -1,16 +1,9 @@
|
||||
package main
|
||||
import "fmt"
|
||||
package numerical
|
||||
|
||||
//using recursion
|
||||
func fibo(num int) int {
|
||||
if num <= 1 {
|
||||
return num
|
||||
}
|
||||
return fibo(num -1) + fibo(num - 2)
|
||||
}
|
||||
|
||||
func main(){
|
||||
num := 10
|
||||
result := fibo(num)
|
||||
fmt.Println(result)
|
||||
if num <= 1 {
|
||||
return num
|
||||
}
|
||||
return fibo(num-1) + fibo(num-2)
|
||||
}
|
||||
|
@ -1,47 +1,48 @@
|
||||
package gcd
|
||||
package numerical
|
||||
|
||||
func gcd(x uint, y uint) uint {
|
||||
|
||||
var shift uint = 0
|
||||
// GCD returns gcd of x and y
|
||||
func GCD(x uint, y uint) uint {
|
||||
|
||||
if x == y {
|
||||
return x
|
||||
}
|
||||
var shift uint = 0
|
||||
|
||||
if x == 0 {
|
||||
return y
|
||||
}
|
||||
if x == y {
|
||||
return x
|
||||
}
|
||||
|
||||
if y == 0 {
|
||||
return x
|
||||
}
|
||||
if x == 0 {
|
||||
return y
|
||||
}
|
||||
|
||||
for shift := 0; (x | y) & 1 == 0; shift++ {
|
||||
x = x >> 1
|
||||
y = y >> 1
|
||||
}
|
||||
if y == 0 {
|
||||
return x
|
||||
}
|
||||
|
||||
for ; (x & 1) == 0 ; {
|
||||
x = x >> 1
|
||||
}
|
||||
for shift := 0; (x|y)&1 == 0; shift++ {
|
||||
x = x >> 1
|
||||
y = y >> 1
|
||||
}
|
||||
|
||||
for ; y == 0 ; {
|
||||
|
||||
for ; (y & 1) == 0 ; {
|
||||
y = y >> 1
|
||||
}
|
||||
for (x & 1) == 0 {
|
||||
x = x >> 1
|
||||
}
|
||||
|
||||
if x > y {
|
||||
t := x
|
||||
x = y
|
||||
y = t
|
||||
}
|
||||
for y == 0 {
|
||||
|
||||
y = y - x
|
||||
for (y & 1) == 0 {
|
||||
y = y >> 1
|
||||
}
|
||||
|
||||
}
|
||||
if x > y {
|
||||
t := x
|
||||
x = y
|
||||
y = t
|
||||
}
|
||||
|
||||
y = y << shift
|
||||
y = y - x
|
||||
|
||||
return y
|
||||
}
|
||||
|
||||
y = y << shift
|
||||
|
||||
return y
|
||||
}
|
||||
|
@ -1,18 +1,19 @@
|
||||
package gcd
|
||||
package numerical
|
||||
|
||||
import "testing"
|
||||
|
||||
func Test_gcd(t *testing.T) {
|
||||
// TestGcd tests gcd
|
||||
func TestGcd(t *testing.T) {
|
||||
|
||||
if gcd(100, 200) != 50 {
|
||||
t.Error("[Error] gcd(100, 200) is wrong")
|
||||
if GCD(100, 200) != 50 {
|
||||
t.Error("[Error] GCD(100, 200) is wrong")
|
||||
}
|
||||
|
||||
if gcd(4, 2) != 1 {
|
||||
t.Error("[Error] gcd(4,2) is wrong")
|
||||
if GCD(4, 2) != 1 {
|
||||
t.Error("[Error] GCD(4,2) is wrong")
|
||||
}
|
||||
|
||||
if gcd(6, 3) != 3 {
|
||||
t.Error("[Error] gcd(6,3) is wrong")
|
||||
if GCD(6, 3) != 3 {
|
||||
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