Q)n! means n
(n
1)
...
3
2
1
For example, 10! = 10
9
...
3
2
1 = 3628800,
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
Find the sum of the digits in the number 100!
Solution:
now since 100! is too large to handled by long, int or any other primitive data type in java , we again use BigInteger library. The solution is very simple:
import java.math.BigInteger;
public class Prob20 {
public static void main(String[] args) {
BigInteger sum = BigInteger.ZERO;
BigInteger fact = BigInteger.ONE;
BigInteger rem;
for(int i = 1 ; i <= 100 ; i++ )
{
fact = fact.multiply(BigInteger.valueOf(i));
}
while(!fact.equals(BigInteger.ZERO))
{
rem= fact.mod(BigInteger.TEN);
fact=fact.divide(BigInteger.TEN);
sum = sum.add(rem);
}
System.out.println(sum);
}
}
No comments:
Post a Comment