我一直在嘗試2^100用 Golang計算。我了解數(shù)字類型的限制并嘗試使用math/big包。這是我嘗試過的,但我不知道為什么它不起作用。我已經(jīng)使用兩種方法的冪計算來計算冪。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 回答

慕姐4208626
TA貢獻(xiàn)1852條經(jīng)驗 獲得超7個贊
計算 2^100
package main
import (
"fmt"
"math/big"
)
func main() {
n := big.NewInt(0)
fmt.Println(n.SetBit(n, 100, 1))
}
- 3 回答
- 0 關(guān)注
- 353 瀏覽
添加回答
舉報
0/150
提交
取消