7

我尝试使用 for 循环和双精度数据类型以典型方式找到大量的阶乘,例如 8785856。

但结果显示无穷大,可能是因为它超出了它的极限。

所以请指导我找到一个非常大的阶乘的方法。

我的代码:

class abc
{
    public static void main (String[]args)
    {
        double fact=1;
        for(int i=1;i<=8785856;i++)
        {
            fact=fact*i;
        }

        System.out.println(fact);
    }
}

输出:-

Infinity

我是 Java 新手,但已经学习了一些 IO 处理等概念。

4

12 回答 12

9
public static void main(String[] args) {
    BigInteger fact = BigInteger.valueOf(1);
    for (int i = 1; i <= 8785856; i++)
        fact = fact.multiply(BigInteger.valueOf(i));
    System.out.println(fact);
}
于 2012-07-12T07:50:06.923 回答
9

您可能需要重新考虑计算这个巨大的价值。Wolfram Alpha 的近似表明它肯定不适合在你的主内存中显示。

于 2012-07-12T07:35:45.540 回答
6

此代码应该可以正常工作:-

public class BigMath {
    public static String factorial(int n) {
        return factorial(n, 300);
    }

    private static String factorial(int n, int maxSize) {
        int res[] = new int[maxSize];
        res[0] = 1; // Initialize result
        int res_size = 1;

        // Apply simple factorial formula n! = 1 * 2 * 3 * 4... * n
        for (int x = 2; x <= n; x++) {
            res_size = multiply(x, res, res_size);
        }

        StringBuffer buff = new StringBuffer();
        for (int i = res_size - 1; i >= 0; i--) {
            buff.append(res[i]);
        }

        return buff.toString();
    }

    /**
     * This function multiplies x with the number represented by res[]. res_size
     * is size of res[] or number of digits in the number represented by res[].
     * This function uses simple school mathematics for multiplication.
     * 
     * This function may value of res_size and returns the new value of res_size.
     */
    private static int multiply(int x, int res[], int res_size) {
        int carry = 0; // Initialize carry.

        // One by one multiply n with individual digits of res[].
        for (int i = 0; i < res_size; i++) {
            int prod = res[i] * x + carry;
            res[i] = prod % 10; // Store last digit of 'prod' in res[]
            carry = prod / 10;  // Put rest in carry
        }

        // Put carry in res and increase result size.
        while (carry != 0) {
            res[res_size] = carry % 10;
            carry = carry / 10;
            res_size++;
        }

        return res_size;
    }

    /** Driver method. */
    public static void main(String[] args) {
        int n = 100;

        System.out.printf("Factorial %d = %s%n", n, factorial(n));
    }
}
于 2015-07-26T11:59:15.423 回答
3

提示:使用BigInteger类,并准备给 JVM 大量内存。的值8785856!是一个非常大的数字。

于 2012-07-12T07:33:21.023 回答
1

使用类BigInteger。(我不确定这是否适用于如此巨大的整数)

于 2012-07-12T07:31:47.700 回答
0

InfinityDouble类中的一个特殊保留值,当您超过 adouble可以容纳的最大数量时使用。

如果您希望您的代码工作,请使用BigDecimal该类,但给定输入数字,不要期望您的程序很快完成执行。

于 2012-07-12T07:35:44.760 回答
0

使用 BigInteger 解决您的问题(8785856!)的上述解决方案将花费数小时的 CPU 时间,如果不是几天的话。您需要确切的结果还是近似值就足够了?

有一种数学方法叫做“ Sterling's Approximation ”,可以简单快速地计算出来,下面是Gosper的改进: 在此处输入图像描述

于 2015-08-31T12:18:30.347 回答
0
    public static void main (String[] args) throws java.lang.Exception
    {
        BigInteger fact= BigInteger.ONE;
        int factorialNo = 8785856 ;

        for (int i = 2; i <= factorialNo; i++) {
              fact = fact.multiply(new BigInteger(String.valueOf(i)));
        }

        System.out.println("Factorial of the given number is = " + fact);
     }
于 2019-10-27T06:37:17.603 回答
0
 import java.util.*;
 import java.math.*;

class main
{
public static void main(String args[])
{
    Scanner sc= new Scanner(System.in);

        int i;
        int n=sc.nextInt();


      BigInteger fact = BigInteger.valueOf(1);

        for ( i = 1; i <= n; i++)
        {
            fact = fact.multiply(BigInteger.valueOf(i));
        }
        System.out.println(fact);

}
}
于 2017-08-07T19:17:45.563 回答
0

尝试这个:

import java.math.BigInteger;

public class LargeFactorial
{
    public static void main(String[] args)
    {
        int n = 50; 
    }
    public static BigInteger factorial(int n)
    {
       BigInteger result = BigInteger.ONE;
       for (int i = 1; i <= n; i++)
           result = result.multiply(new BigInteger(i + ""));
       return result;
    }
}
于 2018-06-10T02:50:22.867 回答
0
    Scanner r = new Scanner(System.in);
    System.out.print("Input Number : ");
    int num = r.nextInt();
    int ans = 1;
    if (num <= 0) {
        ans = 0;
    }
    while (num > 0) {
        System.out.println(num + " x ");
        ans *= num--;
    }
    System.out.println("\b\b=" + ans);
于 2018-11-21T11:00:05.223 回答
-2
import java.util.Scanner;


public class factorial {
    public static void main(String[] args) {
        System.out.println("Enter the number : ");
        Scanner s=new Scanner(System.in);
        int n=s.nextInt();
        factorial f=new factorial();
        int result=f.fact(n);
        System.out.println("factorial of "+n+" is "+result);
    }
    int fact(int a)
    {
        if(a==1)
            return 1;
        else
            return a*fact(a-1);
    }

}
于 2018-04-01T20:20:25.823 回答