MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / IterativePower

Function IterativePower

math/power/fastexponent.go:4–15  ·  view source on GitHub ↗

IterativePower is iterative O(logn) function for pow(x, y)

(n uint, power uint)

Source from the content-addressed store, hash-verified

2
3// IterativePower is iterative O(logn) function for pow(x, y)
4func IterativePower(n uint, power uint) uint {
5 var res uint = 1
6 for power > 0 {
7 if (power & 1) != 0 {
8 res = res * n
9 }
10
11 power = power >> 1
12 n *= n
13 }
14 return res
15}
16
17// RecursivePower is recursive O(logn) function for pow(x, y)
18func RecursivePower(n uint, power uint) uint {

Callers 2

TestIterativePowerFunction · 0.85
BenchmarkIterativePowerFunction · 0.85

Calls

no outgoing calls

Tested by 2

TestIterativePowerFunction · 0.68
BenchmarkIterativePowerFunction · 0.68