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.

## Top comments (36)

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

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.

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.Just did this super quick in JS.

while there are many explanations, easiest to understand was yours. the only part I struggle to understand is this one "isPalindrome(product) && product > largestPalindrome", can you please explain what it means?

Yes. Let's try reading it step by step:

Hope that helps, let me know if you still have questions!

Thank you, Faraz, I didn't know it was possible to use several conditional operators in one single line of code

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.

Love to work on the challenges, please keep this series running.

Here's my Rust Solution: Playground

a lazy c++ solution :)

## First observation is that the number must be between 100^2 and 999^2 or in the range of [10000, 998001].

## As the majority of numbers has 6 digits and we're looking for the largest, we ignore 5 digits numbers.

## Based on this, we can construct a palindromic number as:

## 'abccba' =100000a+10000b+1000c+100c+10b+a

## =100001a+10010b+1100c

## =11(9091a+910b+100c) = p.q

## This equation shows us, that either p or q, but not both must have a factor of 11!!!

def palindrome_number(x,y):

void solve()

{

fast;

ll n,largepalin=0;

cin>>n;

for(ll i=999;i>=100;i--)

{

for(ll j=999;j>=100;j--)

{

if((i*j)%11==0 && palin(i*j) && (i*j)<n) largepalin=max(i*j,largepalin);

}

}

cout<<largepalin<<endl;

return;

}

Sooo... 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).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/b9meWritten in Java!

This is my solution in Javascript

function largestPalindromeProduct(n) {

let num = "1"

let lNumber = 0

for(let i = 0; i < n; i++){

num =

`${num}0`

}

num = parseInt(num)

let rangeNum = num.toString().replace("0","")

lNumberLoop:

for(let i = num; i > 0 ; i--){

for(let j = i; j > i - rangeNum; j --){

let number = (i * j)

let revN = parseInt(number.toString().split("").reverse().join(""))

if(number === revN && revN.toString().length % 2 === 0){

lNumber = number

break lNumberLoop

}

}

}

return lNumber

}

console.log(largestPalindromeProduct(2));

Here is freeCodeCamp version of it

Solution in Python:

Some comments may only be visible to logged-in visitors. Sign in to view all comments.