Posted By

theobriscoe on 12/18/08


Tagged

project factorial Euler


Versions (?)

Find the sum of digits in 100! (Factorial)


 / Published in: Groovy
 

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

  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
Posted By: marcospereira on December 22, 2008

Take a look at: http://snipplr.com/view/10635/a-groovier-find-the-sum-of-digits-in-100-factorial/

You need to login to post a comment.