re: Project Euler #4 - Largest Palindrome Product VIEW POST


A naive F# implementation:

let reverse (s : string) = s |> Seq.toArray |> Array.rev |> System.String

let palindrome n = let s = string n in s = reverse s

let main _ =
    seq { for i = 100 to 999  do
              for j = 100 to 999 do
                  let p = i * j
                  if palindrome p then yield p }
    |> Seq.max
    |> printf "%d\n"

Runs reasonably fast with .NET Core on my Macbook Air:

$ time dotnet Euler4.dll
dotnet Euler4.dll  0.35s user 0.03s system 101% cpu 0.376 total

This is another solution that I consider working in this particolar case (3 digits) but not actually correct (granted I could understand F# 😅). I explained it here:


This code generates the sequence of all palindromes which are products of 3 digit numbers and then selects the largest with Seq.max, so it does indeed produce the correct solution (just verify it on the Project Euler website).

You were right though about the Ruby version in your other comment. On a side note though: maybe don't go around telling people their code might be wrong if you can't actually understand the language it's written in?

I'm sorry if that seemed rude, that wasn't my intention at all. I did add "granted I could understand F#" in case I was wrong.

But no, I will keep on trying to understand what's going on in other people's code even out of my comfort zone, because I'll have the chance to learn something new 👌

If your intention really is to learn then you'd be better of asking questions (e.g. "I don't know F#, would you mind explaining how this selects the biggest palindrome?") instead of making statements that need a qualifier of potentially being wrong.

Being wrong is nothing to be ashamed of, as long as you're ready to stand corrected.

My mistake in this case is that I commented while it was very late 😴 Took note on the approach, though.

code of conduct - report abuse