Skip to content

Instantly share code, notes, and snippets.

@eric1234
Created September 23, 2009 00:27
Show Gist options
  • Select an option

  • Save eric1234/191572 to your computer and use it in GitHub Desktop.

Select an option

Save eric1234/191572 to your computer and use it in GitHub Desktop.
Problem 4 on Project Euler
class Numeric
def palindromic?
to_s.reverse == to_s
end
end
class Range
def reverse
ReverseRange.new last, first, exclude_end?
end
end
class ReverseRange < Range
def each &blk
cur = first
blk[cur] until (cur = cur.pred) == last
end
end
largest = (100..999).reverse.collect do |i|
possible = (100..i).reverse.find do |j|
product = i * j
product.palindromic?
end || 0
i * possible
end.max
puts largest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment