英文:
How do you calculate the square root of a Big Int in Go?
问题
我正在尝试在Go语言中计算一个大整数的平方根,但我不确定我是否正确使用了这个函数(甚至是否使用了正确的函数)。
以下是我目前的代码:
package main
import (
"fmt"
"math/big"
)
func main() {
x := big.NewInt(10)
fmt.Print(x.ModSqrt(big.NewInt(2), big.NewInt(1)))
}
我试图计算10的平方根,但这段代码的输出是<nil>
。
请问有人可以解释一下如何正确使用这个方法吗?因为我不理解文档,并且我找不到其他任何可能帮助我理解如何使用这个方法的用法。
英文:
I'm attempting to calculate the square root of a Big Int in Go, but I'm not sure if I'm using the function correctly (or even the correct function).
Here is what I have so far:
package main
import (
"fmt"
"math/big"
)
func main() {
x := big.NewInt(10)
fmt.Print(x.ModSqrt(big.NewInt(2), big.NewInt(1)))
}
I am trying to calculate the square root of 10, but the output of this code is <nil>
.
Can someone please explain how to use this method correctly as I don't understand the documentation and I can't find any usages of this elsewhere that might help me understand how to use the method?
答案1
得分: 2
big
包中没有提供计算平方根的功能。你需要自己实现它。特别是ModSqrt
对你来说是无用的,它是一个模运算的东西。
英文:
The big
package contains nothing for taking square roots. You'll have to implement it yourself. ModSqrt
, in particular, is useless to you; it's a modular arithmetic thing.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论