猿问

如何在 Go 中获得 5000 的阶乘

我想在 Go 中计算 5000 的阶乘,但结果为 0,因为结果大于 uint64。但是,我能够在 Node.js 中使用


const BigNumber = require('big-number').

Go 中有等效项吗?


我所做的是:


func RecursiveFactorial(number int) big.Int {

    if number >= 1 {

        return big.Int{(number) * RecursiveFactorial(number-1)

    } else {

        return 1

    }

}


呼啦一阵风
浏览 143回答 3
3回答

绝地无双

在 Go 中,使用math/big包。例如,// OEIS: A000142: Factorial numbers: n! = 1*2*3*4*...*n.// https://oeis.org/A000045package mainimport (    "fmt"    "math/big")func factorial(x *big.Int) *big.Int {    n := big.NewInt(1)    if x.Cmp(big.NewInt(0)) == 0 {        return n    }    return n.Mul(x, factorial(n.Sub(x, n)))}func main() {    fmt.Println(factorial(big.NewInt(5000)))}游乐场: https: //play.golang.org/p/53TmmygltkR

慕姐8265434

func factorial(n int64) *big.Int {    fac := new(big.Int)    fac.MulRange(1, n)    return fac}来自 math/big 的 z.MulRange(a, b) 计算从所有 int64 a 到 int64 b 的乘积。它使用拆分和递归算法(分而治之)。它比学校阶乘算法快得多。计算 1 000 000!很快 = ~ 8.26393168833e+5565708

至尊宝的传说

你需要使用math/big包。您可以实现计算recursively或iteratively. 在大多数情况下迭代会更快并且产生更少的垃圾。在我的机器上,迭代 impl 的运行速度提高了 3.1 倍,分配的垃圾减少了 2.9 倍。BenchmarkIterAndRecursive/recursive-6&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;3000&nbsp; &nbsp; &nbsp; &nbsp;3891062 ns/op&nbsp; &nbsp; 17181056 B/op&nbsp; &nbsp; &nbsp; 15003 allocs/opBenchmarkIterAndRecursive/iterative-6&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 10000&nbsp; &nbsp; &nbsp; &nbsp;1237597 ns/op&nbsp; &nbsp; &nbsp; 656089 B/op&nbsp; &nbsp; &nbsp; &nbsp;5172 allocs/oppackage mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "log"&nbsp; &nbsp; "math/big"&nbsp; &nbsp; "testing")func main() {&nbsp; &nbsp; fmt.Println(factorial(big.NewInt(5000)))&nbsp; &nbsp; fmt.Println(factorialIter(5000))}func TestIterWorkTheSame(t *testing.T) {&nbsp; &nbsp; recursive := factorial(big.NewInt(5000))&nbsp; &nbsp; iterative := factorialIter(5000)&nbsp; &nbsp; if recursive.Cmp(iterative) != 0 {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatalf("Invalid computation, \n[%v]\n[%v]", recursive, iterative)&nbsp; &nbsp; }}func BenchmarkIterAndRecursive(b *testing.B) {&nbsp; &nbsp; b.Run("recursive", func(b2 *testing.B) {&nbsp; &nbsp; &nbsp; &nbsp; for i := 0; i < b2.N; i++ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; factorial(big.NewInt(5000))&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; })&nbsp; &nbsp; b.Run("iterative", func(b2 *testing.B) {&nbsp; &nbsp; &nbsp; &nbsp; for i := 0; i < b2.N; i++ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; factorialIter(5000)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; })}func factorial(x *big.Int) *big.Int {&nbsp; &nbsp; n := big.NewInt(1)&nbsp; &nbsp; if x.Cmp(big.NewInt(0)) == 0 {&nbsp; &nbsp; &nbsp; &nbsp; return n&nbsp; &nbsp; }&nbsp; &nbsp; return n.Mul(x, factorial(n.Sub(x, n)))}func factorialIter(x int) *big.Int {&nbsp; &nbsp; result := big.NewInt(1)&nbsp; &nbsp; for i := 2; i <= x; i++ {&nbsp; &nbsp; &nbsp; &nbsp; result.Mul(result, big.NewInt(int64(i)))&nbsp; &nbsp; }&nbsp; &nbsp; return result}
随时随地看视频慕课网APP

相关分类

Go
我要回答