6

嗨,这是一种阶乘方法,但它在控制台中打印 0 请帮助我,谢谢

public class Demo {

    public static void main(String[] args) {
        Demo obj = new Demo();
        System.out.println(obj.factorial(500));
    }

    public int factorial(int n) {
        int fact = 1;

        for (int i = 2; i <= n; i++) {
            fact= fact*i;
        }
        return fact;
    }

已编辑:将返回 Infinity!

public class Demo {

    public static void main(String[] args) {
        Demo obj = new Demo();
        System.out.println(obj.factorial(500));
    }

    public double  factorial(long n) {
       double fact = 1;

        for (int i = 2; i <= n; i++) {
            fact= fact*i;
        }
        return fact;
    }
}
4

6 回答 6

19

因为500!等于1220136825991110068701238785423046926253574342803192842192413588385845373153881997605496447502203281863013616477148203584163378722078177200480785205159329285477907571939330603772960859086270429174547882424912726344305670173270769461062802310452644218878789465754777149863494367781037644274033827365397471386477878495438489595537537990423241061271326984327745715546309977202781014561081188373709531016356324432987029563896628911658974769572087926928871281780070265174507768410719624390394322536422605234945850129918571501248706961568141625359056693423813008856249246891564126775654481886506593847951775360894005745238940335798476363944905313062323749066445048824665075946735862074637925184200459369692981022263971952597190945217823331756934581508552332820762820023402626907898342451712006207714640979456116127629145951237229913340169552363850942885592018727433795173014586357570828355780158735432768888680120399882384702151467605445407663535984174430480128938313896881639487469658817504506926365338175055478128640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000你不能把它放进一个int(范围高达2147483647)。

  • 使用int您最多只能存储12!.
  • 使用一个long你会起床20!
  • 使用 adouble你会达到170!.

这是使用的解决方案BigInteger

public static BigInteger factorial(int i) {
    BigInteger n = BigInteger.valueOf(i);
    while (--i > 0)
        n = n.multiply(BigInteger.valueOf(i));
    return n;
}
于 2010-10-11T11:20:09.240 回答
4

您无法适应500!32 位int.

对于涉及大数的计算,请考虑使用 adouble或 a BigInteger,具体取决于您需要近似答案还是精确答案。

(实际上,对于500!,即使 adouble也不够:Double.MAX_VALUE是 1.7976931348623157E+308,这将“仅”让您上升到170!

于 2010-10-11T11:15:18.450 回答
2

如果您需要计算阶乘函数,您应该考虑两件事:

1)记忆。这将大大加快您的计算速度,因为阶乘函数具有递归定义。你所做的是缓存以前的计算,所以当你请求时,你可以通过计算你是否缓存k!来一步得到它。k*((k-1)!)(k-1)!

2)斯特林近似。如果您需要计算较大的阶乘,您可以通过这种方式非常快速地逼近它们,并且保证误差范围内,因此您可以判断该逼近对于您的应用程序是否可以接受。

如果你都不做这些,你会发现有一些相对较小k的,你根本无法k!在合理的时间内计算出来。

于 2010-10-11T13:06:15.650 回答
0

Grodriguez 是对的——这几乎肯定是由整数溢出引起的。

如果您使用更适度的输入测试您的方法,它似乎会返回正确的输出:

public static void main(String[] args) {
   Demo obj = new Demo();
   for (int i = 0; i < 10; i++)
      System.out.println(i + "! = " + obj.factorial(i));
} 

500!巨大的;在测试您的功能时,从较小的输入开始将是谨慎的。

于 2010-10-11T11:19:44.840 回答
0

500!太大了,不适合长或
你将不得不使用其他技术来获得这个。

但首先,需要什么样的程序500!

于 2010-10-11T11:22:30.920 回答
0

There are some very nice optimization for the implementation of factorizations: see for instance luschny.de for a nice implementation of them in Java. Some require more mathematical insight then others... Have fun with the library :-)

于 2010-10-11T13:39:29.363 回答