Golang程序 检查给定数字的4次方
例子
例如, 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.