Created
March 20, 2014 18:56
-
-
Save leeacto/9671222 to your computer and use it in GitHub Desktop.
PEuler 34
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Machine | |
attr_accessor :facts | |
def initialize | |
@facts = {} | |
fill_facts | |
end | |
def find_sum | |
upper = @facts['9']*3 | |
cur_sum = 0 | |
(3..upper).each do |j| | |
cur_sum += j if curious?(j) | |
end | |
puts cur_sum | |
end | |
private | |
def fact(num) | |
return 1 if num == 0 | |
tot = 1 | |
num.downto(1) do |i| | |
tot *= i | |
end | |
tot | |
end | |
def fill_facts | |
10.times do |i| | |
facts[i.to_s] = fact(i) | |
end | |
end | |
def curious?(num) | |
num_as_arr = num.to_s.split(//) | |
fact_tot = 0 | |
num_as_arr.each do |n| | |
fact_tot += @facts[n] | |
end | |
fact_tot == num | |
end | |
end | |
m = Machine.new | |
m.find_sum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment