英文:
Find the square root of a big.Int
问题
我需要在我的项目中使用big.Ints,因为我处理的数字超过了int64的限制。
对于普通的int,你可以使用以下代码来计算平方根:
math.Sqrt(value)
但是我不知道如何使用big.Ints来实现这个功能。
非常感谢你的帮助。谢谢!
英文:
I am required to use big.Ints for my project because the numbers I am working with exceed the int64 limit.
With regular ints, you can square it using:
math.Sqrt(value)
But I can't work out how to do this, but with big.Ints.
Any help would be appreciated greatly,
Thanks
答案1
得分: 2
使用big.int接口中提供的https://golang.org/pkg/math/big/#Int.Sqrt函数。
package main
import (
"fmt"
"math/big"
)
func main() {
var Str = `10000000000000000000000000000000000000000000000000000`
bigInt := &big.Int{}
value, _ := bigInt.SetString(Str, 10)
sqrt := bigInt.Sqrt(value)
fmt.Println(sqrt)
}
输出结果:
100000000000000000000000000
英文:
Use https://golang.org/pkg/math/big/#Int.Sqrt given in big.int interface
package main
import (
"fmt"
"math/big"
)
func main() {
var Str = `10000000000000000000000000000000000000000000000000000`
bigInt := &big.Int{}
value, _ := bigInt.SetString(Str, 10)
sqrt := bigInt.Sqrt(value)
fmt.Println(sqrt)
}
Output:
100000000000000000000000000
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论