Skip to content

Problem 34 – Sum of all numbers equal to sum of factorial

May 14, 2010

The solution for this problem is somewhat similar to the 30th problem, as it involves the construction of a list based on the problem rules:

import Char

f = sum [x | x <- [3..50000], x == g x]
  where g = sum . map (factorial . digitToInt) . show
        factorial n = foldl (*) 1 [1..n]

main = print f

Again, the execution was quite fast:

real 0m0.059s
user 0m0.056s
sys 0m0.003s

However, I started with going until 10^6, since I din’t know how many numbers would satisfy the condition. In fact, there are only two, so I could therefore reduce the search domain considerably.

Advertisements
Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: