/*
* bhatt.umang7@gmail.com
*/
/*
* 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!
*/
import java.math.BigInteger;
/**
* @author umang
*/
public class Problem_20
{
public static void main(String args[])
{
BigInteger ans = new BigInteger("1");
for (int i = 1; i <= 100; i++)
{
ans = ans.multiply(new BigInteger(new Integer(i).toString()));
}
long sum = 0;
String s = ans.toString();
for (int i = 0; i < s.length(); i++)
{
sum = sum + Long.parseLong("" + s.charAt(i));
}
System.out.print(sum);
}
}
// ans is 648
|
Hi, I reached here while searching for same problem with 1000 instead of 100 . unfortunately above solution is not suitable for large numbers.
ReplyDelete