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.
热门推荐
10 新人红包祝福语简短精辟
11 幼师给毕业祝福语简短
12 生日高考祝福语妹妹简短
13 下飞机祝福语简短英文
14 元宵发客户祝福语简短
15 爸妈金婚文案祝福语简短
16 新郎朋友红包祝福语简短
17 虎年五一祝福语大全简短
18 出阁宴祝福语姐姐简短