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

Been searching here and google for a couple of days as well as asking my programming friends.

Unfortunately, i still don't understand how to change my code...

My program calculates the factorial of a given number. It then provides a number which represents how many digits the factorials answer includes. Then it sums the values of those digits together to give a total.

My program works for any number between 1! and 31!... If you put in anything over 31! (for example 50! or 100!) it doesn't work and just throws back minus numbers and no total.

I was hoping you guys could point me in the right direction or give me some advice.

I understand using BigIntegers maybe a solution however i don't understand them personally, hence coming here.

Any help would be much appreciated. Thanks.

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

}

}

解决方案

You can use BigInteger in java, it has as much numbers as you want

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

EDIT: the code is improved to be compatible with author's code

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值