您的位置:首页 > 其它

欧拉项目 第20题 Factorial digit sum

2016-03-18 10:42 316 查看
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.
Find the sum of the digits in the number 100!
求100!个位数和。

public static void main(String[] args) {
long start = System.currentTimeMillis();
BigInteger x = BigInteger.ONE;
for(Integer i=2;i<100;i++) {
x = x.multiply(BigInteger.valueOf(i));
//            if(x.divideAndRemainder(new BigInteger("10"))[1].compareTo(new BigInteger("0")) == 0) {
//                x = x.divide(new BigInteger("10"));
//            }
}
Integer sum = 0;
String sx = x.toString().replaceAll("0", "");
for(int i=0;i<sx.length();i++) {
sum += Integer.parseInt(sx.substring(i, i + 1)); //<span style="font-family: monospace; font-size: 12.87px; white-space: pre-wrap;"> Character.getNumericValue(sx.charAt(i));</span>
}
System.out.println(sum);
System.out.println(System.currentTimeMillis() - start);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: