找到一个 big.Int 的平方根。

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

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

huangapple
  • 本文由 发表于 2017年5月6日 21:38:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/43821111.html
匿名

发表评论

匿名网友

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

确定