例如,n = 12 => 12不是4的幂。
例如,n = 64 => 64是4的幂。
解决这个问题的方法
步骤1-定义一个接受数字n的方法。
第2步-除以log(n)log(4),存储在res中。
步骤3-如果res的下限与res相同,则打印n为4的幂。
步骤4-否则,打印n不是4的幂。
package main
import (
"fmt"
"math"
)
func CheckPowerOf4(n int){
res := math.Log(float64(n)) / math.Log(float64(4))
if res == math.Floor(res) {
fmt.Printf("%d is the power of 4.\n", n)
} else {
fmt.Printf("%d is not the power of 4.\n", n)
}
}
func main(){
CheckPowerOf4(13)
CheckPowerOf4(16)
CheckPowerOf4(0)
}输出结果13 is not the power of 4. 16 is the power of 4. 0 is the power of 4.