Some More Euler Project, Problem 4

12 May 2008

This one was pretty straightforward.

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.

The hardest part was just reversing the number to be able to tell if it was a palindrome. Unfortunately, when I searched for <20;reverse string f#<21; I got http://basildoncoder.com/blog/2008/04/21/project-euler-problem-4/ which while certainly a good search result, could have ruined the fun. Anyhow: the solution to reverse (why the heck isn<17;t there a .Reverse on the .net string anyway?) is the obvious thing. The only thing notable thing here is the <20;:string<21; on the argument to the function which is the first time I<17;ve had to write a type other than the type coercions from float <-> int. It reminds me of the irritating problem with C# generics where it’s often not possible to write the generic function because it has to be completely generic, or has to implement an interface so the type can be where-constrained (or in other words, generics are generics, not templates, I guess. The C++ approach definitely has benefits, sometimes, though.)

let rev (s:string) = new string(Array.rev(s.ToCharArray()))

{ for i in [100..999] do
    for j in [100..999] do
        let x = i * j
        if x.ToString() = rev(x.ToString()) then yield x }
|> Seq.fold(max) 0

After that, it’s pretty simple. I just make a list of all the palindromes created from 3 digit multiplicands, and then fold out the maximum of those. I noticed something very dumb in the last solution, I was folding an anonymous function that just directly passed its arguments to max, which is of course silly. I’m finding F#’s syntax a little weird, but getting better.

It occurs to me after reading Mr. Basildon’s solution that I don’t know the difference between Seq, List, etc. and when it matters. Maybe it’s just that seq can be infinite, vs. List is strictly an actual memory block?