lördag 30 juni 2012

Euler 4

def palindrome(n1,n2)
  best = 0
  n2.downto(n1) do |i|
    n2.downto(i) do |j|
      if i*j > best then
        s = (i*j).to_s
        best = i*j if s == s.reverse
      end
    end
  end
  best
end

def assert(expect, actual)
  puts "#{expect} != #{actual}" if expect != actual
end

start = Time.now
assert 9009, palindrome(10,99)
assert 906609, palindrome(100,999)
puts Time.now-start
30 ms.