Merge branch 'master' into depthFirstSearch
commit
215f4b4d7f
@ -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,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,25 @@
|
|||||||
|
package numerical
|
||||||
|
|
||||||
|
// PrimesUpTo finds all prime numbers from 1 to upperBound
|
||||||
|
// It's implemented using eratosthenes sieve
|
||||||
|
// Works in O(upperBound) time and space
|
||||||
|
func PrimesUpTo(upperBound int) []int {
|
||||||
|
// lp array stores minimal prime divisor for every number from 2 to upperBound
|
||||||
|
var lp []int
|
||||||
|
lp = make([]int, upperBound+1)
|
||||||
|
|
||||||
|
// primes array stores primes
|
||||||
|
var primes []int
|
||||||
|
|
||||||
|
for i := 2; i <= upperBound; i++ {
|
||||||
|
if lp[i] == 0 {
|
||||||
|
lp[i] = i
|
||||||
|
primes = append(primes, i)
|
||||||
|
}
|
||||||
|
for j := 0; j < len(primes) && primes[j] <= lp[i] && i*primes[j] <= upperBound; j++ {
|
||||||
|
lp[i*primes[j]] = primes[j]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return primes
|
||||||
|
}
|
@ -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")
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,72 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func getIdx(target int, nodes []int) int {
|
||||||
|
for i := 0; i < len(nodes); i++ {
|
||||||
|
if nodes[i] == target {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
func notExist(target int, slice []int) bool {
|
||||||
|
for i := 0; i < len(slice); i++ {
|
||||||
|
if slice[i] == target {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func breadthFirstSearch(start, end int, nodes []int, edges [][]bool) bool {
|
||||||
|
var route []int
|
||||||
|
var queue []int
|
||||||
|
startIdx := getIdx(start, nodes)
|
||||||
|
queue = append(queue, startIdx)
|
||||||
|
for len(queue) > 0 {
|
||||||
|
now := queue[0]
|
||||||
|
route = append(route, nodes[now])
|
||||||
|
if len(queue) > 1 {
|
||||||
|
queue = queue[1:]
|
||||||
|
} else {
|
||||||
|
queue = queue[0:]
|
||||||
|
}
|
||||||
|
for i := 0; i < len(edges[now]); i++ {
|
||||||
|
if edges[now][i] && notExist(i, queue) {
|
||||||
|
queue = append(queue, i)
|
||||||
|
}
|
||||||
|
edges[now][i] = false
|
||||||
|
edges[i][now] = false
|
||||||
|
}
|
||||||
|
if route[len(route)-1] == end {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
nodes := []int{
|
||||||
|
1, 2, 3, 4, 5, 6,
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
sample graph
|
||||||
|
①-②
|
||||||
|
| |
|
||||||
|
③-④-⑤-⑥
|
||||||
|
*/
|
||||||
|
edges := [][]bool{
|
||||||
|
{false, true, true, false, false, false},
|
||||||
|
{true, false, false, true, false, false},
|
||||||
|
{true, false, false, true, false, false},
|
||||||
|
{false, true, true, false, true, false},
|
||||||
|
{false, false, false, true, false, true},
|
||||||
|
{false, false, false, false, true, false},
|
||||||
|
}
|
||||||
|
start := 1
|
||||||
|
end := 6
|
||||||
|
result := breadthFirstSearch(start, end, nodes, edges)
|
||||||
|
fmt.Println(result)
|
||||||
|
}
|
@ -0,0 +1,47 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/binary"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
const digit = 4
|
||||||
|
const maxbit = -1 << 31
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
var data = []int32{421, 15, -175, 90, -2, 214, -52, -166}
|
||||||
|
fmt.Println("\n--- Unsorted --- \n\n", data)
|
||||||
|
radixsort(data)
|
||||||
|
fmt.Println("\n--- Sorted ---\n\n", data, "\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
func radixsort(data []int32) {
|
||||||
|
buf := bytes.NewBuffer(nil)
|
||||||
|
ds := make([][]byte, len(data))
|
||||||
|
for i, e := range data {
|
||||||
|
binary.Write(buf, binary.LittleEndian, e^maxbit)
|
||||||
|
b := make([]byte, digit)
|
||||||
|
buf.Read(b)
|
||||||
|
ds[i] = b
|
||||||
|
}
|
||||||
|
countingSort := make([][][]byte, 256)
|
||||||
|
for i := 0; i < digit; i++ {
|
||||||
|
for _, b := range ds {
|
||||||
|
countingSort[b[i]] = append(countingSort[b[i]], b)
|
||||||
|
}
|
||||||
|
j := 0
|
||||||
|
for k, bs := range countingSort {
|
||||||
|
copy(ds[j:], bs)
|
||||||
|
j += len(bs)
|
||||||
|
countingSort[k] = bs[:0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var w int32
|
||||||
|
for i, b := range ds {
|
||||||
|
buf.Write(b)
|
||||||
|
binary.Read(buf, binary.LittleEndian, &w)
|
||||||
|
data[i] = w ^ maxbit
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,63 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
// definition of a bst node
|
||||||
|
type node struct {
|
||||||
|
val int
|
||||||
|
left *node
|
||||||
|
right *node
|
||||||
|
}
|
||||||
|
|
||||||
|
// definition of a node
|
||||||
|
type btree struct {
|
||||||
|
root *node
|
||||||
|
}
|
||||||
|
|
||||||
|
// allocating a new node
|
||||||
|
func newNode(val int) *node {
|
||||||
|
return &node{val, nil, nil}
|
||||||
|
}
|
||||||
|
|
||||||
|
// insert nodes into a binary search tree
|
||||||
|
func insert(root *node, val int) *node {
|
||||||
|
if root == nil {
|
||||||
|
return newNode(val)
|
||||||
|
}
|
||||||
|
if val < root.val {
|
||||||
|
root.left = insert(root.left, val)
|
||||||
|
} else {
|
||||||
|
root.right = insert(root.right, val)
|
||||||
|
}
|
||||||
|
return root
|
||||||
|
}
|
||||||
|
|
||||||
|
// inorder traversal algorithm
|
||||||
|
// Copies the elements of the bst to the array in sorted order
|
||||||
|
func inorderCopy(n *node, array []int, index *int) {
|
||||||
|
if n != nil {
|
||||||
|
inorderCopy(n.left, array, index)
|
||||||
|
array[*index] = n.val
|
||||||
|
*index++
|
||||||
|
inorderCopy(n.right, array, index)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func treesort(array []int, tree *btree) {
|
||||||
|
// build the binary search tree
|
||||||
|
for _, element := range array {
|
||||||
|
tree.root = insert(tree.root, element)
|
||||||
|
}
|
||||||
|
index := 0
|
||||||
|
// perform inorder traversal to get the elements in sorted order
|
||||||
|
inorderCopy(tree.root, array, &index)
|
||||||
|
}
|
||||||
|
|
||||||
|
// tester
|
||||||
|
func main() {
|
||||||
|
tree := &btree{nil}
|
||||||
|
numbers := []int{5, 4, 3, 2, 1, -1, 0}
|
||||||
|
fmt.Println("numbers : ", numbers)
|
||||||
|
treesort(numbers, tree)
|
||||||
|
fmt.Println("numbers : ", numbers)
|
||||||
|
}
|
Loading…
Reference in New Issue