英文:
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 (
"fmt"
"os"
"strconv"
"math/big"
)
func main() {
fmt.Print("What integer would you like to to find a total factorial for?")
var userinput string
var userint int
fmt.Scan(&userinput)
userint, err := strconv.Atoi(userinput)
if err != nil {
fmt.Println("ERROR: Please input an integer")
os.Exit(2)
}
var efactorial int = 1
var ofactorial int = 1
var tfactorial int
var counter int
for counter = 2; counter <= userint; counter = counter + 2 {
efactorial = efactorial * counter
}
for counter = 1; counter <= userint; counter = counter + 2 {
ofactorial = ofactorial * counter
}
fmt.Println("Even factorial is: ", efactorial)
fmt.Println("Odd factorial is: ", ofactorial)
tfactorial = efactorial + ofactorial
fmt.Println("The Total factorial is: ", 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 (
"fmt"
"math/big"
)
func main() {
var f big.Int
f.MulRange(1, 50)
fmt.Println(&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 <= userint; counter = counter + 2 {
efactorial.Mul(efactorial * big.NewInt(counter))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论