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.
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下