Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 用递归进行整数阶乘_Go - Fatal编程技术网

Go 用递归进行整数阶乘

Go 用递归进行整数阶乘,go,Go,我正在尝试实现以下代码: func factorial(x int) (result int) { if x == 0 { result = 1; } else { result = x * factorial(x - 1); } return; } 作为一个大的.Int,以使其对更大的x值有效 下面返回fmt.Println(阶乘(r))的值0 7的阶乘应该是5040 你知道我做错了什么吗 package main import "fmt" import "

我正在尝试实现以下代码:

func factorial(x int) (result int) {
  if x == 0 {
    result = 1;
  } else {
    result = x * factorial(x - 1);
  }
  return;
}
作为一个大的.Int,以使其对更大的x值有效

下面返回fmt.Println(阶乘(r))的值0

7的阶乘应该是5040

你知道我做错了什么吗

package main

import "fmt"
import "math/big"

func main() {
        fmt.Println("Hello, playground")

    //n := big.NewInt(40)
    r := big.NewInt(7)

    fmt.Println(factorial(r))

}

func factorial(n *big.Int) (result *big.Int) {
    //fmt.Println("n = ", n)
    b := big.NewInt(0)
    c := big.NewInt(1)

    if n.Cmp(b) == -1 {
        result = big.NewInt(1)
    }
    if n.Cmp(b) == 0 {
        result = big.NewInt(1)
    } else {
        // return n * factorial(n - 1);
        fmt.Println("n = ", n)
        result = n.Mul(n, factorial(n.Sub(n, c)))
    }
    return result
}

这个代码在你的
int
版本中,每个
int
都是不同的。但是在你的
big.Int
版本中,你实际上是在共享
big.Int
值。所以当你说

result = n.Mul(n, factorial(n.Sub(n, c)))
表达式
n.Sub(n,c)
实际上将
0
存储回
n
,因此当计算
n.Mul(n,…)
时,基本上是执行
0*1
,结果返回
0

请记住,
big.Int
操作的结果不仅返回它们的值,还将它们存储到接收器中。这就是为什么在诸如
n.Mul(n,c)
之类的表达式中会看到重复,例如,为什么它再次将
n
作为第一个参数。因为你也可以说
result.Mul(n,c)
,你会得到相同的值,但是它会存储在
result
中,而不是
n

以下是为避免此问题而重写的代码:

func factorial(n *big.Int) (result *big.Int) {
    //fmt.Println("n = ", n)
    b := big.NewInt(0)
    c := big.NewInt(1)

    if n.Cmp(b) == -1 {
        result = big.NewInt(1)
    }
    if n.Cmp(b) == 0 {
        result = big.NewInt(1)
    } else {
        // return n * factorial(n - 1);
        fmt.Println("n = ", n)
        result = new(big.Int)
        result.Set(n)
        result.Mul(result, factorial(n.Sub(n, c)))
    }
    return
}
这里是一个稍微更干净/优化的版本(我试图删除
big.Int
s的无关分配):

例如

package main

import (
    "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() {
    r := big.NewInt(7)
    fmt.Println(factorial(r))
}
输出:

5040

Go package
math.big
has
func(*Int)MulRange(a,b int64)
。在第一个参数设置为1的情况下调用时,它将返回b!:

package main

import (
    "fmt"
    "math/big"
)

func main() {
    x := new(big.Int)
    x.MulRange(1, 10)
    fmt.Println(x)
}
将产生

3628800

非递归版本:

func FactorialBig(n uint64) (r *big.Int) {
    //fmt.Println("n = ", n)
    one, bn := big.NewInt(1), new(big.Int).SetUint64(n)
    r = big.NewInt(1)
    if bn.Cmp(one) <= 0 {
        return
    }
    for i := big.NewInt(2); i.Cmp(bn) <= 0; i.Add(i, one) {
        r.Mul(r, i)
    }
    return
}
func factoriaBIG(n uint64)(r*big.Int){
//fmt.Println(“n=”,n)
一,bn:=big.NewInt(1),new(big.Int).SetUint64(n)
r=大。新整数(1)

如果bn.Cmp(一个)谢谢!是的,那些大.Int运算的结果确实有点棘手。@Greg:这里有一个含义,它跳过了递归,直接进入for循环。这就是
x.MulRange
,而不是
x.MulRange
。这里低估了答案。大多数用法可能不需要大.Int作为阶乘方法的输入,只是作为return类型(因为只有答案是非常大的)。如果是这样,MulRange更简单(更快)。