diff --git a/numerical/fast_pow.go b/numerical/fast_pow.go new file mode 100644 index 0000000..1073c42 --- /dev/null +++ b/numerical/fast_pow.go @@ -0,0 +1,16 @@ +package numerical + +//O(log n) function for pow(x, y) +func FastPow(n uint, power uint) uint { + var res uint = 1 + for power > 0 { + + if (power & 1) != 0 { + res = res * n + } + + power = power >> 1 + n = n * n + } + return res +}