LoginSignup
3
3

More than 5 years have passed since last update.

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

Last updated at Posted at 2014-08-25

問題

驚くべき事に、各桁の4乗を足すと元の数になるのは、以下の3つのみである。(ただし1は除く)
1634 = 1^4 + 6^4 + 3^4 + 4^4
8208 = 8^4 + 2^4 + 0^4 + 8^4
9474 = 9^4 + 4^4 + 7^4 + 4^4
これら3つの数の和は、1634 + 8208 + 9474 = 19316である。

同様に、各桁の5乗を足すと元の数になるものたちの和を求めよ。

回答

N桁の数の各桁の5乗を足すと、最大でもN*(9^5)までにしかならない。
9^5=59049なので、N<=6という上限が課せられる。
その上、6*(9^5)=354294なので、2〜354294の範囲で探せばよいことになる。

main = do
     print $  [x| x<-[2..354294], x==sum (map ((^5).read.(:"")) (show x))]

感想

プログラム自体は1行で簡単あるが、探す範囲の上限を見いだすのに時間がかかりました。

3
3
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
3
3