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


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



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.