package xxx.xxx.xxx;
import java.math.BigInteger;
/*
* 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!
*/
public class FactorialDigitSum {
private void compute(int value){
BigInteger product = BigInteger.ONE;
int factor = 2;
while(factor<=value){
product = product.multiply(BigInteger.valueOf(factor));
factor++;
}
int sum = 0;
char[] chars = product.toString().toCharArray();
for(char c:chars){
sum += Integer.parseInt(String.valueOf(c));
}
System.out.println(sum);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FactorialDigitSum factorialDigitSum = new FactorialDigitSum();
factorialDigitSum.compute(100);
}
}
import java.math.BigInteger;
/*
* 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!
*/
public class FactorialDigitSum {
private void compute(int value){
BigInteger product = BigInteger.ONE;
int factor = 2;
while(factor<=value){
product = product.multiply(BigInteger.valueOf(factor));
factor++;
}
int sum = 0;
char[] chars = product.toString().toCharArray();
for(char c:chars){
sum += Integer.parseInt(String.valueOf(c));
}
System.out.println(sum);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FactorialDigitSum factorialDigitSum = new FactorialDigitSum();
factorialDigitSum.compute(100);
}
}