Probelm 34

Problem 34

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.

[code=ruby]require “rubygems”
require “memoize”
include Memoize
def fact(n)
n == 0 ? 1 : n*fact(n-1)
end

memoize :fact

p (3…100000).select {|e|
e.to_s.split(’’).inject(0){|s,i| s += fact(i.to_i)} == e
}.inject(0){|s,e| s += e}[/code]

[quote]def silnia(n)
if n == 0
1
else
n * silnia(n-1)
end
end
puts (3…100_000).select {|e| e == e.to_s.split(’’).map{|i| silnia(i.to_i)}.inject{|s,n| s + n }}.inject{|s,n| s + n }[/quote]
Ostro inspirowane rozwiązaniem teamona z problemu 30.