Project Euler Problem 4 in F#

In the continuing series of solving Project Euler problems with F#, here is problem 4 and my 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.

  1. Create the set of all products of two 3-digit numbers.
  2. Filter the values that are palindromes.
  3. Find the max of the palindrome values.
open System

let problem4 =
    let reverseNumber value = Convert.ToInt32(new string(Array.rev (value.ToString().ToCharArray())))
    [for i in [100..999] do
        for j in [100..999] do yield i * j]
            |> Seq.filter (fun x -> x = reverseNumber(x)) |> Seq.max

Download the source code with unit tests.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s