vRp.CD2g_test/utils/maths/math.go

50 lines
646 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package maths
// Pow 整数幂运算
func Pow(a, n int) int {
if a == 0 {
return 0
}
if n == 0 {
return 1
}
if n == 1 {
return a
}
var result int = 1
factor := a
for n != 0 {
if n&1 != 0 {
// 当前位是 1需要乘进去
result *= factor
}
factor *= factor
n = n >> 1
}
return result
}
// PowInt64 整数幂运算
func PowInt64(a, n int64) int64 {
if a == 0 {
return 0
}
if n == 0 {
return 1
}
if n == 1 {
return a
}
var result int64 = 1
factor := a
for n != 0 {
if n&1 != 0 {
// 当前位是 1需要乘进去
result *= factor
}
factor *= factor
n = n >> 1
}
return result
}