Question
145 is a curious number, as .
Find the sum of all numbers which are equal to the sum of the factorial of their digits.
Note: as and are not sums they are not included.
Clojure
#!/usr/bin/env clojure
(defn factorial [n]
(if (or (= n 1) (= n 0))
1
(* n (factorial (- n 1)))))
(defn digits [number]
(map #(- (int %) 48) (str number)))
(defn curious? [number]
(and (= (apply + (map factorial (digits number))) number)
(> (count (digits number)) 1)))
(println (reduce + (filter curious? (range 50000))))
Haskell
factorial :: Int -> Int
factorial n = product [1..n]
digits :: Int -> [Int]
digits 0 = []
digits n = r : digits q
where (q, r) = quotRem n 10
curious :: Int -> Bool
curious n = n == sum (map factorial (digits n))
main :: IO ()
main = print $ sum $ filter curious [10..50000]
Ruby
#!/usr/bin/env ruby
puts (0..50000).select { |i|
i.to_s.length > 1 && i == i.to_s.each_char.map { |d| (1..d.to_i).reduce(1, :*) }.reduce(:+)
}.reduce(:+)