Tuesday, July 17, 2012

java code for eular project problem 20

PROBLEM


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!

ANSWER

648

JAVA CODE


public class Problem20 {
public static void main(String[]args){
int ans=0;
BigInteger val=getNum(100);
String data=val.toString();
for (int i = 0; i < data.length(); i++) {
ans+=Integer.parseInt(new Character(data.charAt(i)).toString());
}
System.out.println(ans);
}

private static BigInteger getNum(int i) {
BigInteger ret=new BigInteger("1");
for (int j = 1; j <= i; j++) {
ret=ret.multiply(new BigInteger(Integer.toString(j)));
}
return ret;
}
}


0 comments:

Post a Comment