Skip to content

Project Euler Problem 4

October 7, 2011

Project Euler Problem 4 can be solved using the following steps of our solve approach:

  1. Sequence of all possible products of 3-digit numbers can be produced by making a cartesian product of all 3-digit numbers, where instead of two numbers their product is placed into the collection; then duplicates are excluded
  2. PassAll is used for select
  3. filter passes only those sequence members that are palindromic
  4. aggregator finds the maximal of such
open System

let allProducts l1 l2 = (fun x -> ( (fun y -> x * y) l2)) l1 |> List.concat

let isAnagram (s: string) =
    new String(s.ToCharArray() |> Array.rev) = s

let isPalindromic n =
    isAnagram(string n)

let problem004 () =
    allProducts [100..999] [100..999] |> Seq.distinct
    |> solve PassAll isPalindromic Seq.max

Or, without solve function the definition of problem004() would be

let problem004 () =
    allProducts [100..999] [100..999]
    |> (Seq.distinct >> Seq.filter isPalindromic >> Seq.max)

From → Project Euler

One Comment

Trackbacks & Pingbacks

  1. Project Euler Problem 70 « In F# Major

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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

%d bloggers like this: