How to use Math/Big in Go Lang

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

How to use Math/Big in Go Lang

问题

我正在尝试创建一个阶乘程序,但是当数字变得太大时,答案就会变得错误。以下是我的代码。我对math/big不熟悉,无法正确地将其实现到程序中。任何帮助都将不胜感激。谢谢。

package main

import (
	"fmt"
	"os"
	"strconv"
	"math/big"
)

func main() {
	fmt.Print("您想计算哪个整数的阶乘?")
	var userinput string
	var userint int
	fmt.Scan(&userinput)
	userint, err := strconv.Atoi(userinput)
	if err != nil {
		fmt.Println("错误:请输入一个整数")
		os.Exit(2)
	}
	var efactorial = big.NewInt(1)
	var ofactorial = big.NewInt(1)
	var tfactorial = big.NewInt(0)
	var counter int

	for counter = 2; counter <= userint; counter = counter + 2 {
		efactorial.Mul(efactorial, big.NewInt(int64(counter)))
	}

	for counter = 1; counter <= userint; counter = counter + 2 {
		ofactorial.Mul(ofactorial, big.NewInt(int64(counter)))
	}
	fmt.Println("偶数阶乘为:", efactorial)
	fmt.Println("奇数阶乘为:", ofactorial)

	tfactorial.Add(efactorial, ofactorial)
	fmt.Println("总阶乘为:", tfactorial)
}

这是修改后的代码,使用了math/big包来处理大数运算。现在应该能正确计算大数的阶乘了。希望对你有帮助!

英文:

I am trying to create a factorial program, but when the numbers get too big the answer becomes wrong. Here is my code. I am new to math/big and cannot figure out how to correctly implement it into the program. Any help is appreciated. Thanks.

package main

import (
&quot;fmt&quot;
&quot;os&quot;
&quot;strconv&quot;
&quot;math/big&quot;
)

func main() {
fmt.Print(&quot;What integer would you like to to find a total factorial for?&quot;)
var userinput string
var userint int
fmt.Scan(&amp;userinput)
userint, err := strconv.Atoi(userinput)
if err != nil {
	fmt.Println(&quot;ERROR: Please input an integer&quot;)
	os.Exit(2)
}
var efactorial int = 1
var ofactorial int = 1
var tfactorial int
var counter int

for counter = 2; counter &lt;= userint; counter = counter + 2 {
	efactorial = efactorial * counter
}

for counter = 1; counter &lt;= userint; counter = counter + 2 {
	ofactorial = ofactorial * counter
}
fmt.Println(&quot;Even factorial is: &quot;, efactorial)
fmt.Println(&quot;Odd factorial is: &quot;, ofactorial)

tfactorial = efactorial + ofactorial
fmt.Println(&quot;The Total factorial is: &quot;, tfactorial)
}

答案1

得分: 11

你可以使用big.Int.MulRange来计算一系列整数的乘积。这对于计算阶乘非常理想。这里有一个计算50!的完整示例

package main

import (
	"fmt"
	"math/big"
)

func main() {
	var f big.Int
	f.MulRange(1, 50)
	fmt.Println(&f)
}

输出结果为:

30414093201713378043612608166064768844377641568960512000000000000
英文:

You can use big.Int.MulRange to find the product of a range of integers. This is ideal for computing factorials. Here's a complete example that computes 50!

package main

import (
	&quot;fmt&quot;
	&quot;math/big&quot;
)

func main() {
	var f big.Int
	f.MulRange(1, 50)
	fmt.Println(&amp;f)
}

The output:

30414093201713378043612608166064768844377641568960512000000000000

答案2

得分: 1

你希望ofactorial和tfactorial的类型为big.Int。

ofactorial := big.NewInt(1)
tfactorial := big.NewInt(0)

然后,你可以使用big包中的方法来进行Ints的乘法操作,可以在这里找到相关方法。

你的for循环应该类似于:

for counter = 2; counter <= userint; counter = counter + 2 {
    efactorial.Mul(efactorial, big.NewInt(counter))
}
英文:

you want ofactorial and tfactorial to be of type big.Int

ofactorial := big.NewInt(1)
tfactorial := big.NewInt(0)

Then you will want to use the methods from the big package for multiplying Ints found here

your for loop will look something like

for counter = 2; counter &lt;= userint; counter = counter + 2 {
    efactorial.Mul(efactorial * big.NewInt(counter))
}

huangapple
  • 本文由 发表于 2017年3月3日 07:30:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/42567954.html
匿名

发表评论

匿名网友

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

确定