Posted By

marcospereira on 12/22/08


Tagged

factorial sum bigdecimal idiomatic


Versions (?)

Find the sum of digits in 100! (Factorial): A little more groovier


 / Published in: Groovy
 

  1. import static java.math.BigInteger.ONE
  2.  
  3. def factorial2(BigInteger n) {
  4.  
  5. def answer = ONE
  6.  
  7. for (i=BigInteger.ONE; i<=n; i++) {
  8. answer *= i
  9. }
  10.  
  11. return answer
  12. }
  13.  
  14. def sumDigits(BigInteger n) {
  15.  
  16. def digitString = n.toString()
  17. def digits = digitString.collect { it.toBigInteger() }
  18.  
  19. return digits.sum()
  20.  
  21. }

Report this snippet  

You need to login to post a comment.