2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Project Eulerをhaskellで練習していく日記: Problem 34

Posted at

#問題
145は、各桁の階乗の和が元の数にかる希有な数である。
1! + 4! + 5! = 1 + 24 + 120 = 145

このように、各桁の階乗の和が元の数になるような全ての数の和を求めよ。
ただし、1=1!と、2=2!は右辺が和ではないので、除外する。
#回答

-- 9までの階乗をあらかじめ計算しておきます。
facts = scanl (*) 1 [1..9]

-- 最大何桁まで調べれば良いかを計算します。
keta = maximum $ takeWhile (\n->n*(facts !! 9)>10^n) [n|n<-[2..]]

-- 各桁の階乗の和が元の数になるかどうか
isSum n = n == sum[facts !! (read (x:"")) | x<-(show n)]

main = do
     print $ sum $ filter isSum [10..10^keta]

#感想
今回も上限を見極めるのがちょっとややこしかった。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?