Project Euler Problem 8

Another ruby solution in my ongoing efforts to master this wonderful technology. This time, it’s for problem 8.

I’m focusing very much on the ability ruby gives you to write not only write *less* code, but to at the same time write code that is more readable, particularly to non-programmers. Half way through writing this solution, i came to the realization that I was basically writing English! I am going to try and do this to ridiculous levels for the remainder of the problems I solve 🙂

Anyway, onto the solution. I didn’t use my Math mixin this time, mainly because I became a little obsessed with making the code read like English half way through. So here is the solution;

#note: this first bit just gets the numbers into an array

the_numbers = "73167176531330624919225119674426574742355349194
93496983520312774506326239578318016984801869478851843858615607
89112949495459501737958331952853208805511125406987471585238630
50715693290963295227443043557668966489504452445231617318564030
98711121722383113622298934233803081353362766142828064444866452
38749303589072962904915604407723907138105158593079608667017242
71218839987979087922749219016997208880937766572733300105336788
12202354218097512545405947522435258490771167055601360483958644
67063244157221553975369781797784617406495514929086256932197846
86224828397224137565705605749026140797296865241453510047482166
37048440319989000889524345065854122758866688116427171479924442
92823086346567481391912316282458617866458359124566529476545682
84891288314260769004224219022671055626321111109370544217506941
65896040807198403850962455444362981230987879927244284909188845
80156166097919133875499200524063689912560717606058861164671094
05077541002256983155200055935729725716362695618826704282524836
00823257530420752963450".scan(/\w/)

highest_found = product_of the_numbers.last 5

999.times do
    
    the_numbers.pop      #note: this just gets rid of the last number
    
    product_of_last_five = product_of the_numbers.last 5

    highest_found = product_of_last_five if product_of_last_five > highest_found
    
end

print highest_found

I would be incredibly interested in hearing from anyone who has no coding experience at all, but who can still understand what the above does (or anyone who can’t, where did i go wrong?). Sometimes I guess developers can get a bit too close to the forest to see the trees and I am really striving for code readable by non-techies in all of this (will save me a LOT of headaches in my job if I can perfect it, believe me!)

I also used this helper method that will now go into the mixin.

  def product_of array
    total = 1
    array.each do |x| total = total * x.to_i end
    return total
  end
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s