使用BigInteger类的递归查找阶乘 [英] Finding the factorial using recursion with the BigInteger Class

查看:91
本文介绍了使用BigInteger类的递归查找阶乘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So consider the following program-segment! I've tried to use the basic recursion function to determine the factorial of a number, but now using the BigInteger class.

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对象是不可变的?还是我在这里想念东西?

So when I try implementing this in a program, it returns the output as 1. Is it because BigInteger objects are immutable? Or am I missing something here?

推荐答案

代码中有错误,您应该输入

There's an error in the code, you should put

  BigInteger factz = BigInteger.valueOf(a);

而不是BigInteger factz = BigInteger.ONE;

这篇关于使用BigInteger类的递归查找阶乘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆