In the above problem, given a positive integer n
it is intended to find the sum of all the digits in n!
. So here is my java code for it:
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
while(sc.hasNext())
{
int n = sc.nextInt();
BigInteger b = BigInteger.valueOf(1);
for(int i=2;i<=n;i++)
b=b.multiply(BigInteger.valueOf(i));
String s = b.toString();
int sum=0;
for(int i=0;i<s.length();i++)
sum+=(int)(s.charAt(i)-'0');
System.out.println(sum);
}
}
The limits for n is n<=1000
. And it works flawlessly:
INPUT
5
60
100
1000
OUTPUT
3
288
648
10539
But the online judge is judging this as a wrong answer. Is there any problem in using the BigInteger
class?
NOTE:I want the fault in the Implementation of the BigInteger class in my program, as this solution didn't exceed the time-limit but gave a wrong answer.