Uva 10220 - I Love Big Numbers !


 // Accepted
import java.util.*;
import java.math.*;

class Main
{
 public static void main (String[] args)
 {
  Scanner in = new Scanner(System.in);
  
  BigInteger fact [] = new BigInteger [5000];
  
  BigInteger f = fact[1] =  BigInteger.valueOf(1);
  
  for(int i=2;i<1001;i++)
  {
   fact[i] = f.multiply(BigInteger.valueOf(i));
   f=fact[i];
  }
  
  //System.out.println(fact[n]);
  
  while(in.hasNext())
  {
   int n = in.nextInt();
   
   BigInteger tmp = fact[n];
   //System.out.println(tmp);
   BigInteger a=tmp,s=BigInteger.valueOf(0),b=tmp;
   
   while(a!=BigInteger.valueOf(0))
   {
    a=a.mod(BigInteger.valueOf(10));
    s=s.add(a);
    b=b.divide(BigInteger.valueOf(10));
    a=b;
   }
   
   System.out.println(s);
  }
 }
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LeetCode Palindrome Number

Hacker Rank The Power Sum