How do you calculate the square root of a Big Int in Go?

huangapple go评论75阅读模式
英文:

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 (
	&quot;fmt&quot;
	&quot;math/big&quot;
)

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 &lt;nil&gt;.

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.

huangapple
  • 本文由 发表于 2017年1月2日 07:50:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/41420049.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定