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.
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 BigInts, 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 label in JavaScript. Sue me 😂 (also useful if you have to break out of more than one cycle... which is usually rare).
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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 usingBigInt
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 label in JavaScript. Sue me 😂 (also useful if you have to break out of more than one cycle... which is usually rare).