Project Euler Problem 40

Project Euler Problem 40

In my opinion, this was the easiest problem so far. The brute-force method is easy to implement and runs very fast.

This is the code:

$fraction = ''
 
1.upto(1000000) do |i|
  $fraction << i.to_s
end
 
def d_(n)
  $fraction[n-1].to_i
end
 
puts d_(1) * d_(10) * d_(100) * d_(1000) * d_(10000) * d_(100000) * d_(1000000)

It runs in slightly lesser than one second.

The last line isn’t very Ruby-ish because I copied it from the problem description page, because I’m too lazy to write it myself. That’s also why I have a d_(n) function.

Tags: ,

Leave a Reply