微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Java中计算大于整数和long的阶乘?

如何解决在Java中计算大于整数和long的阶乘?

您可以在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

:改进了代码,使其与作者的代码兼容

解决方法

在这里和Google搜索了几天,并询问了我的编程朋友。不幸的是,我仍然不知道如何更改我的代码…

我的程序计算给定数字的阶乘。然后提供一个数字,该数字代表析因答案包括的位数。然后,将这些数字的值相加,得出总数。

我的程序适用于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);

      }
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。