Skip to content

Instantly share code, notes, and snippets.

@myokoym
Created August 16, 2012 13:21
Show Gist options
  • Save myokoym/3370051 to your computer and use it in GitHub Desktop.
Save myokoym/3370051 to your computer and use it in GitHub Desktop.
Problem034
#! ruby
def fact(n)
n == 0 ? 1 : n * fact(n - 1)
end
a = (0..9).map {|v| [v.to_s, fact(v)] }
facts = Hash[*a.flatten(1)]
ans = 3.upto(9999999).select {|n| n.to_s.split(//).map {|v| facts[v] }.inject(:+) == n }
p ans
p ans.inject(:+)
__END__
1.upto(10000) do |n|
x = ("9" * n)
y = x.split(//).map {|v| fact(v.to_i) }.inject(:+)
if x.to_i > y
p n
break
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment