欧拉项目 第20题 Factorial digit sum

来源:互联网 发布:个性挂历制作软件 编辑:程序博客网 时间:2024/06/16 02:21

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


0 0