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 兄妹拍照祝福语简短的话