Question:
For a 1000-digit number, find the 13 adjacent digits which gives out the largest product.
My code goes like this:
series = '731...752963450' # the 1000 digit, simplified here.
# Set up variables.
adjacent_count = 13
pointer = 0
candidates = [], products_array = []
pointer_end = series.length - (adjacent_count - 1)
# Get all candidates
begin
candidates << series[pointer..pointer + adjacent_count - 1]
pointer += 1
end while pointer < pointer_end
# Eliminate All candidates with 0s
candidates = candidates.map {|s| s.include?('0') ? nil : s}.compact
# do the multiplication and compare
candidates.each_with_index do |s, index|
char_array = s.chars
product = 1
char_array.each do |int|
product = product * int.to_i
end
products_array[index] = product
end
answer = products_array.max
equation = candidates[products_array.each_with_index.max[1]].chars * 'x'
puts "Maximum product from this series of digits is #{equation} = #{answer}"
Well I don't know whether I need to eliminate the candidates that contains zeroes in it or just directly go multiply all of those. Not sure if it will save up time using map
method first, Any thoughts?
And for the last bit I tried to print out which candidate brings up this largest product so I created another array to store it. It seems it can be done in a more elegant way. Any thoughts?