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.
Solution
# 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.
# placeholder
answer = 0;
#loop through each 3-digit number
for i in 100..999 do
for j in i..999 do
product = i*j
# the palindrome test is simple here
# since we are using the same technique
# as the F# and C# functions
if product.to_s().reverse == product.to_s() &&
answer < product
answer = product
end
end
end
puts answer
Discussion
I have to admit, Ruby makes this code pretty simple.
If you have questions, leave a comment or please find me on Twitter (@azzlsoft) or email (rich@azzlsoft.com).
Leave a Reply