Skip to content

Commit 8e74480

Browse files
author
zesani
authored
Create power.go
1 parent 20e9877 commit 8e74480

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Math/Power/power.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package power
2+
3+
func Power(x float64, n int) float64 {
4+
if n == 0 {
5+
return 1.00
6+
}
7+
if n == 1 {
8+
return x
9+
}
10+
ans := power(x, n/2)
11+
if n%2 == 0 {
12+
return ans * ans
13+
} else if n < 0 {
14+
return ans * ans / x
15+
} else {
16+
return ans * ans * x
17+
}
18+
}

0 commit comments

Comments
 (0)