简体   繁体   English

golang中的100阶乘

[英]100 factorial in golang

I want to calculate 100 factorial in golang.我想在 golang 中计算 100 阶乘。 Here's the code I'm using.这是我正在使用的代码。

var fact big.Int
fact.MulRange(1, 100)

Printing output gives打印输出给出

30414093201713378043612608166064768844377641568960512000000000000

But googling 100!但是谷歌搜索100! gives 9.332622e+157 .给出9.332622e+157 I figured this is probably because of the datatype I am using (or maybe not).我想这可能是因为我使用的数据类型(或者可能不是)。 How do I solve it?我该如何解决? Thanks in advance.提前致谢。

Edit: So I ran this code in go playground and it gave the correct answer.编辑:所以我在 go playground 中运行了这段代码,它给出了正确的答案。 Is this due to a limitation on my PC?这是由于我的 PC 上的限制吗? Plus when I convert it to string and iterate through it, it shows different numbers另外,当我将其转换为字符串并对其进行迭代时,它会显示不同的数字

str := fact.String()
for _,val := range str{
    fmt.Print(val)
}

Here is all the code这是所有的代码

package main

import (
    "fmt"
    "math/big"
)

func main() {
    var fact big.Int
    fact.MulRange(1, 100)
    fmt.Println(fact)
    n := fact.String()
    fmt.Println(n) //printing 100!
    sum := 0
    for _, i := range n {
        sum += int(i) //sum of each digits in 100!
    }
    fmt.Println(sum)
}

and here is what go env shows:这是 go env 显示的内容:

set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\user\AppData\Local\go-build
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\user\go
set GORACE=
set GOROOT=C:\Go
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\user\AppData\Local\Temp\go-build839268890=/tmp/go-build -gno-record-gcc-switches

and go version : go version go1.10.1 windows/amd64和 go 版本:go 版本 go1.10.1 windows/amd64

To print a string value, simply pass it as-is to fmt.Println() :要打印string值,只需将其按原样传递给fmt.Println()

str := fact.String()
fmt.Println(str)

Also note that you don't need to call its String() method, the fmt package will do that for you.另请注意,您不需要调用它的String()方法, fmt包会为您完成。 But not if you just pass fact to it, because Int.String() has pointer receiver, so you have to pass a pointer to it:但如果您只是将fact传递给它,则不会,因为Int.String()具有指针接收器,因此您必须将指针传递给它:

fmt.Println(&fact)

Or declare and use *big.Int in the first place, and then you can pass fact simply for printing:或者首先声明并使用*big.Int ,然后您可以简单地传递fact以进行打印:

var fact = new(big.Int)
fact.MulRange(1, 100)
fmt.Println(fact)

Actually, since all methods of big.Int have pointer receivers, you should always declare and use pointers of big.Int to avoid surprises.实际上,由于big.Int所有方法都有指针接收者,因此您应该始终声明和使用big.Int指针以避免意外。

Note:笔记:

Your original code does not print what you want because for range on a string ranges over its runes (characters), and rune is an alias for int32 , so the characters of the result will be printed as individual numbers without spaces between them (because you print each with a fmt.Print() call).您的原始代码不打印你想要什么,因为for range上的绳子范围超过其符文(字符), rune是一个别名int32 ,所以结果的字符将打印为单独的数字彼此之间没有空格(因为你使用fmt.Print()调用打印每个)。

For this same reason, to calculate the sum of digits, you have to convert the runes to the numerical value of the digits they represent.出于同样的原因,要计算数字总和,您必须将符文转换为它们代表的数字的数值。 For this, you may simply use digit - '0' :为此,您可以简单地使用digit - '0'

str := fact.String()
sum := 0
for _, val := range str {
    sum += int(val - '0')
}
fmt.Println(sum)

This will print (tr it on the Go Playground ):这将打印(在Go Playground 上显示):

93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
648

credit source : quora信用来源:quora

you can approach it this way你可以这样处理

𝑛!=∏𝑛𝑘=1𝑘

ln(𝑎.𝑏)=ln(𝑎)+ln(𝑏)→ln(𝑛!)=ln(∏𝑛𝑘=1𝑘)=∑𝑛𝑘=1(ln𝑛)

So you can calculate the sum of the logs instead of multiplying all the numbers and then 𝑒𝑥𝑝() the result to get 𝑛!因此,您可以计算对数的总和,而不是将所有数字相乘,然后 𝑒𝑥𝑝() 得到结果以得到 𝑛! . .

𝑒ln(𝑛!)=𝑛! 𝑒ln(𝑛!)=𝑛!

package main

import (
    "fmt"
    "math"
)

func main() {
    fmt.Println("Hello, playground")
    fact(100)
}
func fact(n float64){
var sum float64
sum = 0
var i float64
for i= 1;i<=n;i++{
    sum = sum+math.Log(i)

}
fmt.Println(math.Exp(sum))

}

https://play.golang.org/p/74LPoIifNZ- https://play.golang.org/p/74LPoIifNZ-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM