diff --git a/build/leetcode_library.template b/build/leetcode_library.template index 3382de0..5d33989 100644 --- a/build/leetcode_library.template +++ b/build/leetcode_library.template @@ -16,7 +16,7 @@ ////////////////////////////////////////////////////////////////////// //////////////////////// Dijkstra high-level //////////////////////// -// You can find this data structure overview in docs/algorithms/dijkstra.md +// You can find this algorithm overview in docs/algorithms/dijkstra.md // DijkstraToTarget finds the shortest path from sourceId to targetId in a sourceId connected component func DijkstraToTarget(edges map[int]map[int]int, sourceId int, targetId int) int { @@ -86,7 +86,7 @@ type dijkstraEdge struct { //////////////////////// Eager dijkstra high-level //////////////////////// -// You can find this data structure overview in docs/algorithms/dijkstra.md +// You can find this algorithm overview in docs/algorithms/dijkstra.md // EagerDijkstraToTarget finds the shortest path from sourceId to targetId in a sourceId connected component func EagerDijkstraToTarget(edges map[int]map[int]int, sourceId int, targetId int) int { @@ -221,6 +221,57 @@ func (h *EagerDijkstraIndexedHeap) getByNodeId(toNodeId int) (dijkstraEdge, bool return h.values[i], true } +////////////////////////// Greatest common divider (GCD) ////////////////////////// +// You can find this algorithms overview in docs/algorithms/math.md + +func GCD(a, b int) int { + for b != 0 { + t := b + b = a % b + a = t + } + + return a +} + +////////////////////////// Primes factorization ////////////////////////// +// You can find this algorithms overview in docs/algorithms/math.md + +func GetPrimeFactors(number int, maxNumber int) []int { + sieve := buildPrimeDividers(maxNumber) + primeFactors := make([]int, 0) + + for number != 1 { + factor := sieve[number] + for number%factor == 0 { + number /= factor + } + + primeFactors = append(primeFactors, factor) + } + + return primeFactors +} + +func buildPrimeDividers(maxNumber int) []int { + sieve := make([]int, maxNumber+1) + + for i := 2; i <= maxNumber; i++ { + if sieve[i] != 0 { + continue + } + + sieve[i] = i + for j := i * i; j <= maxNumber; j += i { + if sieve[j] == 0 { + sieve[j] = i + } + } + } + + return sieve +} + ////////////////////// AVL tree ////////////////////// // You can find this data structure overview in docs/data_structures/avl_tree.md