Problem 34 - 階乗してもいっしょ

ガリガリいっちゃうよ。

Problem 34 - Project Euler

145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.

Find the sum of all numbers which are equal to the sum of the factorial of their digits.

Note: as 1! = 1 and 2! = 2 are not sums they are not included.

145は1! + 4! + 5! = 1 + 24 + 120 = 145となるとても奇妙な数です。

各桁の階乗の和が、元の数と等しくなるような整数の総和を求めよ。

ノート: 1! = 1、2! = 2は含みません。

なんか適当だな・・・。


終端が書いてない。何処まで書けばいいのかわからずなんだけど。とりあえず行ってみる。

number->listは略。

(define (problem34 n)
  (if (= n 2)
      '()
      (let ((m (apply + (map (lambda (n)
                               (apply * (iota n 1)))
                             (number->list n)))))
           (if (= m n)
               (cons n (problem34 (- n 1)))
               (problem34 (- n 1))))))

(problem34 1000000) ; (40585 145)
(apply + (problem34 1000000)) ; 40730

回答は出たけれど、終端について疑問が残る。


おぉっと、素晴らしい解説ページ発見。

M.Hiroi's Home Page / xyzzy Lisp Programming

各桁の階乗の和( 階乗和とします )は最大で 9! * n 桁になります。たとえば、8 桁の整数の最小値は 10,000,000 ですが、階乗和の最大値は 9! * 8 = 2,903,040 にしかなりません。つまり、7 桁まで調べればいいわけです。したがって、整数の上限値は 9! * 7 = 2540160 となります。

ほぅほぅ。と思って桁を増やしたら、おせぇぇぇ。


高速化についての解説もあるので、読んでおこうと思う。

今日はここまでぇ〜。

おぉ。今日だけで10問解けた!!

やれば出来る子なのかも(親バカ)