阶乘法效果不好!

发布于 2024-09-27 09:59:33 字数 746 浏览 1 评论 0原文

你好 这是一个阶乘方法,但它在控制台中打印 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;
    }

编辑:将返回无穷大!

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;
    }
}

Hi
this is a factorial method but it prints 0 in the console please help me thanks

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;
    }

EDITED:will return 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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

智商已欠费 2024-10-04 09:59:33

由于 500! 等于 ,因此您无法将其放入 int(范围最大为 2147483647)中。

  • 使用 int 最多只能存储 12!
  • 使用long,您将获得20!
  • 使用double,您将获得170!
  • 按照惯例 0!等于 1;

这是使用 BigInteger 的解决方案:

public static BigInteger factorial(int i) {
    if (i == 0) {
        return BigInteger.ONE;
    }
    BigInteger n = BigInteger.valueOf(i);
    while (--i > 0) {
        n = n.multiply(BigInteger.valueOf(i));
    }
    return n;
}

Since 500! equals 1220136825991110068701238785423046926253574342803192842192413588385845373153881997605496447502203281863013616477148203584163378722078177200480785205159329285477907571939330603772960859086270429174547882424912726344305670173270769461062802310452644218878789465754777149863494367781037644274033827365397471386477878495438489595537537990423241061271326984327745715546309977202781014561081188373709531016356324432987029563896628911658974769572087926928871281780070265174507768410719624390394322536422605234945850129918571501248706961568141625359056693423813008856249246891564126775654481886506593847951775360894005745238940335798476363944905313062323749066445048824665075946735862074637925184200459369692981022263971952597190945217823331756934581508552332820762820023402626907898342451712006207714640979456116127629145951237229913340169552363850942885592018727433795173014586357570828355780158735432768888680120399882384702151467605445407663535984174430480128938313896881639487469658817504506926365338175055478128640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 you can't fit it into an int (which ranges up to 2147483647).

  • Using an int you can only store up to 12!.
  • Using a long you'll get up to 20!
  • Using a double you'll get up to 170!.
  • By convention 0! equals 1;

Here is a solution using BigInteger:

public static BigInteger factorial(int i) {
    if (i == 0) {
        return BigInteger.ONE;
    }
    BigInteger n = BigInteger.valueOf(i);
    while (--i > 0) {
        n = n.multiply(BigInteger.valueOf(i));
    }
    return n;
}
倚栏听风 2024-10-04 09:59:33

您无法将 500! 放入 32 位 int 中。

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

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

There's no way you can fit 500! on a 32-bit int.

For calculations involving large numbers, consider using a double or a BigInteger, depending on whether you want an approximate or an exact answer.

(Actually, for 500!, even a double would not be enough: Double.MAX_VALUE is 1.7976931348623157E+308, which will "only" let you go up to 170!)

撩动你心 2024-10-04 09:59:33

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

1)记忆化 。这将极大地加快计算速度,因为阶乘函数具有递归定义。你所做的就是缓存之前的计算,所以当你请求k!时,你可以通过计算k*((k-1)!) if您已缓存 (k-1)!

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

如果您不执行这些操作,您会发现存在一些相对较小的 k,您根本无法在合理的时间内计算出 k!

There are two things you should be looking into if you need to calculate the factorial function:

1) Memoization. This will dramatically speed up your calculations, since the factorial function has a recursive definition. What you do is cache previous calculations, so when you ask for k!, you can get it in one step by calculating k*((k-1)!) if you have (k-1)! cached.

2) Stirling's approximation. If you need to calculate large factorials, you can approximate them very rapidly this way, and with guaranteed bounds on the error, so you can tell whether the approximation will be acceptably close for your application.

If you do neither of these, you will find that there is some relatively small k for which you simply can't calculate k! in a reasonable amount of time.

染年凉城似染瑾 2024-10-04 09:59:33

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! is ma​​ssive;当测试你的函数时,从较小的输入开始是谨慎的。

Grodriguez is right - this is almost certainly caused by integer overflow.

If you test your method with more modest inputs it appears to return the right output:

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! is massive; when testing your function, starting with smaller inputs would be prudent.

愛放△進行李 2024-10-04 09:59:33

500! 远远太大,不适合长款或双款。
您必须使用其他技术才能获得此结果。

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

500! is way too big to fit a long, or double.
You would have to use other techniques to get this.

But first, what kind of program needs 500!?

等风来 2024-10-04 09:59:33

对于分解的实现有一些非常好的优化:例如参见 luschny.de 在 Java 中很好地实现了它们。有些需要比其他更多的数学洞察力......享受图书馆的乐趣:-)

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 :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文