Factorial in Java

后端 未结 7 1377
庸人自扰
庸人自扰 2020-12-11 08:08

I\'ve been using this factorial program for Java:

public static long factorial(int a) {

    if(a<1) {
        return 1;
    }
    long result=1;
    long         


        
相关标签:
7条回答
  • 2020-12-11 09:02

    Another approach that's less naive and works for non-integer numbers is to use the natural log of the gamma function.

    http://www.iro.umontreal.ca/~simardr/ssj/doc/html/umontreal/iro/lecuyer/util/Num.html

    If you must persist in using this implementation, I'd recommend that you look into memoization. Why keep recalculating values? Once you have one, hang onto it and just hand it out on repeat requests.

    0 讨论(0)
提交回复
热议问题