A Groovier Way to Find the sum of digits in 100! (Factorial)


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

A Groovier way to write http://snipplr.com/view/10565/find-the-sum-of-digits-in-100-factorial/


Copy this code and paste it in your HTML
  1. import static java.math.BigInteger.ONE
  2.  
  3. def factorial2(n) {
  4. (ONE..n).inject(ONE) { result, number -> result*number }
  5. }
  6.  
  7. def sumDigits(n) {
  8. n.toString().collect { it.toBigInteger() }.sum()
  9. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.