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.

## Discussion

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.

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.

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.Here is my code on C (Brute force), the result was out after 0.172s.

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!

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 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;

}

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

Here's my Rust Solution: Playground

Solution in Python:

Here is freeCodeCamp version of it

Written 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));

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/b9meComplete Solution..!!! for Euler 004

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;

}

## 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.

## This is the easiest way and logical too..

here is my result using JS. avoided any use of methods. Also not a master in mathematics but here it is

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. :)

Here's my code C++

/*Largest palindrome product

Problem 4

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.

*/

## include

using namespace std;

int rev(int n);

int main()

{

int mult,largest ,p1=0,p2=0;

for(int i=100;i<=999;i++)

{

for(int j=100;j<=999;j++)

{

mult=i*j;

}

cout<<p1<<"*"<<p2<<" = "<<largest;

return 0;

}

//function to reverse the multiple

int rev(int n)

{

int r=0;

while(n!=0)

{

r=(r*10)+n%10;

n/=10;

}

return r; //we reverse the multiple without any changed in variable mult

}

C++>>Output >> 995*583 = 580085