Last active
August 1, 2017 15:59
-
-
Save mame-n/6455c4a4296f69c365bcc123d241bb06 to your computer and use it in GitHub Desktop.
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 P74 | |
def initialize | |
@max = 1000000 | |
@factorials = make_factorial | |
@sum_factorials = [] | |
@num_loops = [] | |
end | |
def main | |
each_loop_num.size | |
end | |
def each_loop_num | |
( 0 .. @max ).select do |base| | |
@num_loops[base] = find_num_loop( base, tmp_array = [] ) | |
@num_loops[base] == 60 | |
end | |
end | |
def find_num_loop( base, stack ) | |
return @num_loops[base] if @num_loops[base] | |
return 0 if stack.include?( base ) | |
find_num_loop( sum_factorial( base ), stack << base ) + 1 | |
end | |
def sum_factorial( base ) | |
if @sum_factorials[base] | |
@sum_factorials[base] | |
else | |
@sum_factorials[base] = base.to_s.each_char.inject(0) { |sum, ch| sum + @factorials[ ch.to_i ] } | |
end | |
end | |
def make_factorial | |
( 0..9 ).map do |n| | |
( 1 .. n ).inject(1) { |result, i| result * i } | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment