forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfastexponent.go
38 lines (34 loc) · 817 Bytes
/
fastexponent.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package power
// IterativePower is iterative O(logn) function for pow(x, y)
func IterativePower(n uint, power uint) uint {
var res uint = 1
for power > 0 {
if (power & 1) != 0 {
res = res * n
}
power = power >> 1
n *= n
}
return res
}
// RecursivePower is recursive O(logn) function for pow(x, y)
func RecursivePower(n uint, power uint) uint {
if power == 0 {
return 1
}
var temp = RecursivePower(n, power/2)
if power%2 == 0 {
return temp * temp
}
return n * temp * temp
}
// RecursivePower1 is recursive O(n) function for pow(x, y)
func RecursivePower1(n uint, power uint) uint {
if power == 0 {
return 1
} else if power%2 == 0 {
return RecursivePower1(n, power/2) * RecursivePower1(n, power/2)
} else {
return n * RecursivePower1(n, power/2) * RecursivePower1(n, power/2)
}
}