Project Euler: Problem 20

数を稼ぐために、ひとまず簡単そうなのから先に潰していく方針に変更。Problem 20。

n! means n * (n - 1) * ... * 3 * 2 * 1

Find the sum of the digits in the number 100!

http://projecteuler.net/index.php?section=problems&id=20

100! の計算結果に含まれる数字の合計を求めよとのこと。

Gauche で書いた。

#!/usr/bin/env gosh

(define (fact n)
  (let loop ((n n)
             (r 1))
    (if (zero? n)
      r
      (loop (- n 1) (* r n)))))

(define (main args)
  (let loop ((num (fact 100))
             (sum 0))
    (if (< 0 num)
      (loop (quotient num 10)
            (+ sum (remainder num 10)))
      (print sum)))
  0)

適当に (fact n) を求めたうえで、適当に各桁の合計を足し込んでみた、だけ。