project_euler/4.rb
Code:
# Project Euler Problem 4: Largest Palindrome Product
# https://projecteuler.net/problem=4
# A palindromic number reads the same both ways.
# The largest palindrome made from the product of two 2-digit numbers is
# 9009 = 91 × 99.
# Find the largest palindrome made from the product of two 3-digit numbers.
def palindrome?(n)
n.to_s == n.to_s.reverse
end
def solve(digits)
solutions = []
(10 ** digits).times do |n|
(10 ** digits).times do |m|
solutions << n * m if palindrome?(n * m)
end
end
return solutions.max
end
puts solve(2)
puts solve(3)
Output:
9009
906609