# Project Euler #4 - Largest Palindrome Product

### Peter Kim Frank ・1 min read

Project Euler (7 Part Series)

1) Project Euler #1 - Multiples of 3 and 5
2) Project Euler #2 - Even Fibonacci numbers
3 ... 5
3) Project Euler #3 - Largest Prime Factor
4) Project Euler #4 - Largest Palindrome Product
5) Project Euler #5 - Finding the Smallest Multiple
6) Project Euler #6 - Sum Square Difference
7) Project Euler #7 - 10001st prime

I'm bumping a "series" that I started last year, where we collectively work on problems from Project Euler.

This is Problem 4, finding the 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.

Classic DEV Post from Jun 4 '18

Here's my Python solution!

How's the run time? I think yours is Python 2?

Here's mine in Python 3 :) It's a bit more complicated because I try to write them more flexible. Like how this problem shows 2 digit numbers as an example and 3 for the actual problem, so I wrote it to take the number of digits per number as an input

A naive F# implementation:

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

This is another solution that I consider

workingin this particolar case (3 digits) but not actuallycorrect(granted I could understand F# 😅). I explained it here: dev.to/maxart2501/comment/b9meThis 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.

My typescript solution:

Please don't do this 😕 It's verbose for no reason. You can accomplish the same with just

`return condition;`

.cool! yes so much simple

thanks!

Here is my code on C (Brute force), the result was out after 0.172s.

Enhanced:

Here's my Ruby solution. There's probably a much more clever way to do this. Probably a more Ruby way to do it for that matter.

If I understand it correctly (correct me if I'm wrong, I don't know Ruby), this doesn't work in general, as it prints the

firstpalindrome product you find. But you don't know if it's thelargest.Unless you can prove it is 🤷♂️ (I have no idea).

I'm working backwards through the range of numbers beginning with '999.' Hence the extra verbosity in the block with the call to the downto method.

(999..1).each dodoesn't work, and(1..999).each doreally shouldn't work either because ranges are not defined by their content, just a beginning state and an end state. So counting backwards the first palindrome I find is the largest. And the outer block isn't necessary, but I include it just for the sake of being thorough I guess.The problem here is that the products you're getting aren't ordered. Which means that if you get a palindrome, you cannot be sure it's the largest.

In fact, I run your code and I got

`999 * 91 = 90909`

, which is not correct. Even if you limit your range to 999 to 100 (we're looking for 3-digit factors after all), you'd get`995 * 583 = 580085`

. But the largest palindrome is`993 * 913 = 906609`

, which comes after the others.A quadratic solution in JavaScript. I'm curious if there's a way to do this in linear time:

Not linear... but I

think(without any actual proof 🤷♂️) (I'm a fraud 🤦♂️) that my solution does it in logarithmic time: dev.to/maxart2501/comment/b9m6Edit: scratch that, no way it's not quadratic 😂 But then again, it's faster than the extensive check.

Written in Java!

Am still able to just copy and paste from my Euler GitHub repo. 😂

PHP again

This is another solution that I consider

workingin this particular case (3 digits) but not actuallycorrectin the general case. I explained it here: dev.to/maxart2501/comment/b9meSooo... there are so many good solutions, but they all kind of look the same, so I wanted a different approach 😁

What if, instead of starting with the factors and checking if their product is a palindrome, we start with palindromes and find two 3-digit factors?

We'd start with 999999, then 998899, then 997799... You get the point. So we can start with a 3-digit number (e.g. 356)... and

get a palindrome out of it(e.g. 356653), and look for factors.My solution in JavaScript:

I've tested it with up to 7 digits and it's still basically instantaneous. Over that limit you go over

`Number.MAX_SAFE_INTEGER`

and it becomes unreliable. I didn't bother using`BigInt`

s, also because you can't get the square root out of them (you can approximate it, but it's a bother).P.S.: yes,

I've used a labelin JavaScript. Sue me 😂 (also useful if you have to break out of more than one cycle... which is usually rare).Love to work on the challenges, please keep this series running.

Here's my Rust Solution: Playground

Just did this super quick in JS.

Did you try hackerrank's version? They have broader ranges for input values and pretty tight time/memory limitations. That sometimes makes even trivial project euler tasks challenging. :)