java - 在java中计算大于整数和长整数的阶乘?

标签 java biginteger factorial

我在这里和谷歌搜索了几天,并询问了我的编程 friend 。 不幸的是,我仍然不明白如何更改我的代码...

我的程序计算给定数字的阶乘。然后它提供一个数字,表示阶乘答案包含多少位数字。然后它将这些数字的值加在一起得出总数。

我的程序适用于 1 之间的任何数字!和 31!...如果你输入任何超过 31 的东西! (例如 50!或 100!)它不起作用,只会返回负数而不返回总数。

我希望你们能给我指出正确的方向或给我一些建议。 我知道使用 BigIntegers 可能是一种解决方案,但我个人并不了解它们,因此来到这里。

任何帮助将不胜感激。谢谢。

    package java20;

    /**
    * Program to calculate the factorial of a given number.
    * Once implemented, it will calculate how many digits the answer includes.
    * It will then sum these digits together to provide a total.
     * @author shardy
     * date: 30/09/2012
     */

    //import java.math.BigInteger;
    public class Java20 {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {

    //Using given number stored in factorialNo, calculates factorial
    //currently only works for numbers between 1! and 31! :(
        int fact= 1;
        int factorialNo = 10;

        for (int i = 1; i <= factorialNo; i++)
            {
               fact=fact*i;
            }

        System.out.println("The factorial of " + factorialNo + 
                " (or " + factorialNo + "!) is: " + fact);

        //Using answer stored in fact, calculates how many digits the answer has
        final int answerNo = fact;
        final int digits = 1 + (int)Math.floor(Math.log10(answerNo));

        System.out.println("The number of digits in the factorials "
                + "answer is: " + digits);        

        //Using remainders, calculates each digits value and sums them together
        int number = fact;
        int reminder;
        int sum = 0;

        while(number>=1)
            {
             reminder=number%10; 
             sum=sum+reminder;
             number=number/10;
            }

        System.out.println("The total sum of all the " + digits 
                + " idividual digits from the answer of the factorial of " 
                + factorialNo + " is: " + sum);

      }
    }

最佳答案

你可以在java中使用BigInteger,它有多少你想要的数

    BigInteger fact= BigInteger.ONE;
    int factorialNo = 10;

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

    System.out.println("The factorial of " + factorialNo +
                                " (or " + factorialNo + "!) is: " + fact);
   final int digits = fact.toString().length();

   BigInteger number = new BigInteger(fact.toString());
   BigInteger reminder;
   BigInteger sum = BigInteger.ZERO;
   BigInteger ten = new BigInteger(String.valueOf(10));

   while(number.compareTo(BigInteger.ONE)>=0)
     {
     reminder=number.mod(ten);
     sum=sum.add(reminder);
     number=number.divide(ten);
     }

     System.out.println("The total sum of all the " + digits
                     + " idividual digits from the answer of the factorial of "
                     + factorialNo + " is: " + sum

编辑:改进代码以与作者的代码兼容

关于java - 在java中计算大于整数和长整数的阶乘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12661427/

相关文章:

java - 静态映射的空指针异常

Java swing 在其他组件上绘制对象

java - 删除数组java中的特殊字符[]

c - 在带有 while 循环的 C 中使用递归函数的阶乘程序 c

complexity-theory - 递归阶乘程序的复杂性

c - 如何使我的阶乘函数可重入?

java - 可以从网站检查用户计算机上的文件吗?

java.math.BigInteger pow(exponent) 问题

encoding - Go、DER 和处理大整数

mysql - SQL - BIGINT 数据类型总是 8 个字节吗?