Skip to content

Commit

Permalink
Merge pull request #31 from ridwanfathin/master
Browse files Browse the repository at this point in the history
add fast pow: O(log n) exponent
  • Loading branch information
0xAX authored Oct 11, 2020
2 parents b1e2c28 + 0780e4b commit e1dcce7
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions numerical/fast_pow.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit e1dcce7

Please sign in to comment.