Find the sum of digits in 100! (Factorial)


/ Published in: Groovy
Save to your folder(s)

Find the sum of digits in 100!
n! means n × (n − 1) × ... × 3 × 2 × 1


Copy this code and paste it in your HTML
  1. def factorial2(BigInteger n){
  2. BigInteger answer = BigInteger.ONE;
  3.  
  4. for (int i=1; i<=n; i++) {
  5. answer = answer.multiply(BigInteger.valueOf(i));
  6. }
  7. return answer
  8. }
  9.  
  10. def sumDigits(BigInteger n){
  11. def digitString = n.toString();
  12. def digitArray = []
  13.  
  14. digitString.each{ i ->
  15. digitArray.add( i.toBigInteger() )
  16. }
  17. return digitArray.sum()
  18. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.