Problem Definition
Available at Largest Palindrome Product
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.
Implementation
We have to find out a palindrome p such that —
where both a and b are three digit number. To do so, we first define a function that checks whether a number (e.g., p) is a palindrome.
let ispalindrom (x:int):bool = | |
let s = x.ToString() | |
s = (s |> (fun x -> new string(x.ToCharArray() |> Array.rev))) |
Then, we iterate over all the tuples (a,b) of three digit numbers e.g., [100..999] that satisfy the following equation.
Finally, we check if a*b is a palindrome and get the largest palindrome, as outlined below.
seq{ | |
for x in 100..999 do | |
for y in x..999 do | |
if ispalindrom (x*y) then yield (x*y)} | |
|> Seq.max |
Would/did you solve it differently? Please let me know your opinion in the comment section below.
Happy problem solving …