我一直在尝试2^100用 Golang计算。我了解数字类型的限制并尝试使用math/big包。这是我尝试过的,但我不知道为什么它不起作用。我已经使用两种方法的幂计算来计算幂。package mainimport ( "fmt" "math/big")func main() { two := big.NewInt(2) hundred := big.NewInt(50) fmt.Printf("2 ** 100 is %d\n", ExpByPowOfTwo(two, hundred))}func ExpByPowOfTwo(base, power *big.Int) *big.Int { result := big.NewInt(1) zero := big.NewInt(0) for power != zero { if modBy2(power) != zero { multiply(result, base) } power = divideBy2(power) base = multiply(base, base) } return result}func modBy2(x *big.Int) *big.Int { return big.NewInt(0).Mod(x, big.NewInt(2))}func divideBy2(x *big.Int) *big.Int { return big.NewInt(0).Div(x, big.NewInt(2))}func multiply(x, y *big.Int) *big.Int { return big.NewInt(0).Mul(x, y)}
3 回答
![?](http://img1.sycdn.imooc.com/54586453000163bd02200220-100-100.jpg)
慕姐4208626
TA贡献1852条经验 获得超7个赞
计算 2^100
package main
import (
"fmt"
"math/big"
)
func main() {
n := big.NewInt(0)
fmt.Println(n.SetBit(n, 100, 1))
}
- 3 回答
- 0 关注
- 331 浏览
添加回答
举报
0/150
提交
取消