在Java中查找大数的阶乘

问题描述 投票:7回答:13

我试图找到大量的阶乘,例如8785856以典型方式使用for-loop和double数据类型。

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

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

我的代码:

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处理的概念。

java factorial largenumber
13个回答
8
投票
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);
}

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

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

-1
投票

要真正找出这个数字的阶乘,你应该使用PYTHON函数,并尝试打开任务管理器,看看编译器需要多少内存。之后你会知道JVM要花多少时间,因为PYTHON是数值计算的最佳语言。


-1
投票
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);
    }

}

9
投票

您可能想重新考虑计算这个巨大的价值。 Wolfram Alpha's Approximation建议它肯定不适合你的主存储器显示。


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

3
投票

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


1
投票

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


1
投票

这个blog post用例子解释了java中的biginteger factorial。


0
投票

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

如果您希望代码工作,请使用BigDecimal类,但是根据输入编号,不要指望您的程序很快就能完成执行。


0
投票

使用BigInteger解决问题的上述解决方案(8785856!)如果不是几天,则需要几个小时的CPU时间。您需要确切的结果还是近似值?

有一种称为“Sterling's Approximation”的数学方法可以简单快速地计算,以下是Gosper的改进:enter image description here


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

}
}
© www.soinside.com 2019 - 2024. All rights reserved.