solution.scm 780 B

123456789101112131415161718192021222324252627282930
  1. ;;; Factorial digit sum
  2. ;;; Problem 20
  3. ;;; n! means n × (n - 1) × ... × 3 × 2 × 1
  4. ;;; For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
  5. ;;; and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
  6. ;;; Find the sum of the digits in the number 100!
  7. ;; Idea: Seems simple. Just calculate 100! and take the digit sum. I
  8. ;; do not know, whether there is a trick in there to calculate the
  9. ;; digit sum more efficiently, but it is definitely very simple to
  10. ;; calculate the digit sum of large integers in GNU Guile.
  11. (import
  12. (except (rnrs base) let-values map)
  13. (only (guile)
  14. lambda* λ)
  15. (prefix (math) math:)
  16. (debug-utils))
  17. (displayln "sum of digits of 100!:"
  18. (math:digits-sum
  19. (math:factorial-linear 100)))