英文:
Golang Factorial Shows Wrong Result
问题
我已经将一个Python的阶乘函数转换成了一个Go语言程序。Python代码如下:
def main():
n = input('Enter a number')
result = factorial(n)
print(result)
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
而Go语言的代码如下:
package main
import "fmt"
func main() {
fmt.Println("Enter a Number ")
var n int64
var result int64
fmt.Scan(&n)
result = factorial(n)
fmt.Println(result)
}
func factorial(n int64) int64 {
if n == 0 {
return int64(1)
}
return int64(n * factorial(n-1))
}
对于小数字,两个程序的结果是相同的。但对于较大的数字,Go语言的输出结果为零。终端的输出如下:
enKats-MacBook-Air:Factorials venkat$ python factorial.py
Enter a number5
120
VenKats-MacBook-Air:Factorials venkat$ go run factorial.go
Enter a Number
5
120
VenKats-MacBook-Air:Factorials venkat$ python factorial.py
Enter a number99
933262154439441526816992388562667004907159682643816214685929638952175999932299156089414639761565182862536979208272237582511852109168640000000000000000000000
VenKats-MacBook-Air:Factorials venkat$ go run factorial.go
Enter a Number
99
0
VenKats-MacBook-Air:Factorials venkat$
对于较大的数字,Go语言程序出现了问题。请问Go语言程序的问题出在哪里?感谢您的帮助。
英文:
I have converted a Python factorial function into a golang program . The Python code is as follows
def main():
n = input('Enter a number')
result = factorial(n)
print(result)
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
And the Golang problem is as follows ,
package main
import "fmt"
func main() {
fmt.Println("Enter a Number ")
var n int64
var result int64
fmt.Scan(&n)
result = factorial(n)
fmt.Println(result)
}
func factorial(n int64) int64 {
//fmt.Println(n)
if n == 0 {
return int64(1)
}
//fmt.Println(n * factorial(n-1))
return int64(n * factorial(n-1))
}
The results of both the programs are same for small numbers.But for bigger numbers The Go output is zero. The Terminal output is as follows ,
enKats-MacBook-Air:Factorials venkat$ python factorial.py
Enter a number5
120
VenKats-MacBook-Air:Factorials venkat$ go run factorial.go
Enter a Number
5
120
VenKats-MacBook-Air:Factorials venkat$ python factorial.py
Enter a number99
933262154439441526816992388562667004907159682643816214685929638952175999932299156089414639761565182862536979208272237582511852109168640000000000000000000000
VenKats-MacBook-Air:Factorials venkat$ go run factorial.go
Enter a Number
99
0
VenKats-MacBook-Air:Factorials venkat$
What's wrong with the Go Program for larger numbers? Any help would be appreciated.
答案1
得分: 5
这是溢出的结果。普通的 Python int
是任意大小的。但是如果你使用有大小限制的整数,例如使用 numpy
的 64 位整数,你会遇到同样的问题:
>>> import numpy as np
>>> def factorial(n):
... if n == 0:
... return np.int64(1)
... else:
... return n * factorial(n - np.int64(1))
...
>>> factorial(99)
__main__:5: RuntimeWarning: overflow encountered in long_scalars
0
我不是一个 Golang 程序员,但是快速搜索得到了以下标准库的信息:
https://golang.org/pkg/math/big/
英文:
This is the result of overflow. Vanilla Python int
s are arbitrarily sized. But if you use a sized int, e.g. 64 bit-ints using numpy
for example, you get the same problem:
>>> import numpy as np
>>> def factorial(n):
... if n == 0:
... return np.int64(1)
... else:
... return n * factorial(n - np.int64(1))
...
>>> factorial(99)
__main__:5: RuntimeWarning: overflow encountered in long_scalars
0
I am not a golang programmer, but a quick search yielded the following from the standard library:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论