我想用我的 java 程序计算 50 的阶乘而不使用数组

i want to calculate factorial of 50 with my java program without using array

这是我的程序吧

`class Factorial
{
public static void main(String dt[])
{
    long n=50;
    long fact=1;
    if(n==0)
    {
        System.out.println("Factorial is 1");
    }
    else if(n==1)
    {
        System.out.println("Factorial is 1");
    }
    else
    {
        fact=n;
        while(n>1)
        {
            fact=fact*(n-1);
            n--;
        }
        System.out.print("Factorial is "+fact);
    }
}
}`

它不计算 50.i 的阶乘 想在不使用数组和大整数的情况下计算它

阶乘是一个增长非常快到无穷大的数学函数,你的代码是错误的,因为使用多头是为了做空那个范围(到 21!你已经溢出了)

您需要使用其他数据类型,例如大整数。

使用 BigInteger 的重构方法如下所示:

private static void factorial(long n) {
    long x = n;
    BigInteger fact = BigInteger.ONE;
    if ((n == 0) || (n == 1)) {
        System.out.println("Factorial is 1");
    } else {
        fact = BigInteger.valueOf(n);
        while (n > 1) {
            fact = fact.multiply(BigInteger.valueOf(n - 1));
            n--;
        }
        System.out.println("Factorial of " + x + " is " + fact);
    }
}

注意 21 的输出!直到 50 岁!可以是 verified online here

Factorial of 21 is 51090942171709440000

Factorial of 22 is 1124000727777607680000

Factorial of 23 is 25852016738884976640000

Factorial of 24 is 620448401733239439360000

Factorial of 25 is 15511210043330985984000000

Factorial of 26 is 403291461126605635584000000

Factorial of 27 is 10888869450418352160768000000

Factorial of 28 is 304888344611713860501504000000

Factorial of 29 is 8841761993739701954543616000000

Factorial of 30 is 265252859812191058636308480000000

Factorial of 31 is 8222838654177922817725562880000000

Factorial of 32 is 263130836933693530167218012160000000

Factorial of 33 is 8683317618811886495518194401280000000

Factorial of 34 is 295232799039604140847618609643520000000

Factorial of 35 is 10333147966386144929666651337523200000000

Factorial of 36 is 371993326789901217467999448150835200000000

Factorial of 37 is 13763753091226345046315979581580902400000000

Factorial of 38 is 523022617466601111760007224100074291200000000

Factorial of 39 is 20397882081197443358640281739902897356800000000

Factorial of 40 is 815915283247897734345611269596115894272000000000

Factorial of 41 is 33452526613163807108170062053440751665152000000000

Factorial of 42 is 1405006117752879898543142606244511569936384000000000

Factorial of 43 is 60415263063373835637355132068513997507264512000000000

Factorial of 44 is 2658271574788448768043625811014615890319638528000000000

Factorial of 45 is 119622220865480194561963161495657715064383733760000000000

Factorial of 46 is 5502622159812088949850305428800254892961651752960000000000

Factorial of 47 is 258623241511168180642964355153611979969197632389120000000000

Factorial of 48 is 12413915592536072670862289047373375038521486354677760000000000

Factorial of 49 is 608281864034267560872252163321295376887552831379210240000000000

Factorial of 50 is 30414093201713378043612608166064768844377641568960512000000000000

      public static void main(String[] args) {

        long n=50;
        BigInteger fact= BigInteger.valueOf(1);

            for(int i=1;i<n-1;i++)
            {
                fact = fact.multiply(BigInteger.valueOf(n - 1));
            }
            System.out.print("Factorial is "+fact);
      }

您的程序的输出超过了长实例可以保存的最大值。因此,您可以使用 java.math 包的 BigInteger 而不是 long,它可以容纳更大的值。