Skip to content

Instantly share code, notes, and snippets.

@leeacto
Created March 20, 2014 18:56
Show Gist options
  • Save leeacto/9671222 to your computer and use it in GitHub Desktop.
Save leeacto/9671222 to your computer and use it in GitHub Desktop.
PEuler 34
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