6

因此,请考虑以下程序段!我尝试使用基本递归函数来确定数字的阶乘,但现在使用 BigInteger 类。

public static BigInteger fact(int a)
{
    BigInteger factorial = BigInteger.ONE;

    BigInteger factz = BigInteger.ONE;

    if(a == 1)
    {
        return factorial;
    }

    else
    {
        return factz.multiply(fact(a-1));
    }
}

因此,当我尝试在程序中实现它时,它会将输出返回为 1。是因为 BigInteger 对象是不可变的吗?或者我在这里错过了什么?

4

6 回答 6

4

代码有错误,你应该把

  BigInteger factz = BigInteger.valueOf(a);

代替BigInteger factz = BigInteger.ONE;

于 2013-07-28T12:40:33.563 回答
3

递归计算阶乘的伪代码如下:

function factorial(n) {
   if (n == 0)
      return 1;
   else
      return n * factorial(n - 1);
}

实施它将BigInteger是:

public static BigInteger factorial(BigInteger n) {
    if (n.equals(BigInteger.ZERO))
        return BigInteger.ONE;
    else
        return n.multiply(factorial(n.subtract(BigInteger.ONE)));
}

public static void main(String[] args) {
    System.out.println(factorial(new BigInteger("100")));
}

输出将是:

93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

注意:如果很大,递归会占用太多内存n。在这种情况下,最好使用一些迭代算法来计算阶乘。

于 2016-12-28T20:55:06.620 回答
1

我没有得到局部变量的相关性,你需要使用BigInteger.valueOf(a).

你的方法可以用一行来表达:

public static BigInteger fact(int a) {
    return a == 1 ? BigInteger.ONE : BigInteger.valueOf(a).multiply(fact(a - 1));
}
于 2013-07-28T13:40:10.523 回答
0

查找有和没有任何数字递归的阶乘。

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter no to find factorial :");
    BigInteger inputNo1 = input.nextBigInteger();
    System.out.println("With recursion    " + inputNo1 + "! Factorial = " + (factorial(inputNo1.intValue())));
    System.out.println("Without recursion " + inputNo1 + "! Factorial = " + (findFactorial(inputNo1)));
}

private static String findFactorial(BigInteger inputNo1) {
    int counter;
    BigInteger increment = new BigInteger("1");
    BigInteger fact = new BigInteger("1");
    for (counter = 1; counter <= inputNo1.longValueExact(); counter++) {
        fact = fact.multiply(increment);
        increment = increment.add(BigInteger.ONE);
    }
    return String.valueOf(fact);
}

public static BigInteger factorial(int number) {
    if (number <= 1)
        return BigInteger.ONE;
    else
        return factorial(number - 1).multiply(BigInteger.valueOf(number));
}
于 2016-12-28T12:58:28.667 回答
0

我使用 BigInteger 类递归查找阶乘的解决方案

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.*;
import java.util.*;

class Main {
    public static String factorial(int n,String s){
        if(n>0){
            BigInteger fact = new BigInteger(s);
            fact = fact.multiply(new BigInteger(n + ""));
            return factorial(n-1,fact.toString());
        }
        else{
            return s.toString();
        }
    }

    public static void main(String args[] ) throws Exception {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();
            int n = Integer.parseInt(line);
            if(n==0)
            System.out.println("Factorial is 0");
            else{
            String s = factorial(n,"1");
            System.out.println("Factorial is " + s);
            }
    }
}

上述代码的输出截图: 上述代码的输出截图

于 2017-08-23T06:23:49.250 回答
-1

看看这个:

 public static BigInteger fact(BigInteger a)
      {

          if(a.intValue()==1||a.intValue()==0)
          {
              return BigInteger.ONE;
          }

          else
          {
              return a.multiply(fact(a.subtract(BigInteger.ONE)));
          }
      }

修改是:
- 包括0!=1
- 因为函数fact 返回BigInteger,所以它的参数也必须是BigInteger

于 2013-07-28T12:49:39.477 回答