使用BigInteger类的递归查找阶乘

时间:2013-07-28 12:36:49

标签: java recursion biginteger factorial

请考虑以下程序段!我尝试使用基本递归函数来确定数字的阶乘,但现在使用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对象是不可变的?或者我在这里遗漏了什么?

6 个答案:

答案 0 :(得分:3)

代码中出现错误,您应该输入

  BigInteger factz = BigInteger.valueOf(a);

而不是BigInteger factz = BigInteger.ONE;

答案 1 :(得分: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很大,递归会占用太多内存。在这种情况下,最好使用一些迭代算法来计算阶乘。

答案 2 :(得分:1)

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

您的方法只能用一行表示:

public static BigInteger fact(int a) {
    return a == 1 ? BigInteger.ONE : BigInteger.valueOf(a).multiply(fact(a - 1));
}

答案 3 :(得分: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));
}

答案 4 :(得分: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);
            }
    }
}
  

输出以上代码的截图:   Output screenshot for the above code

答案 5 :(得分:-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