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.
Latest comments (36)
C#
here is my result using JS. avoided any use of methods. Also not a master in mathematics but here it is
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..
Complete 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;
}
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;
}
Solution in Python:
Here is freeCodeCamp version of it
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));
a lazy c++ solution :)
Written in Java!
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.
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 π
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: dev.to/maxart2501/comment/b9me
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).
Some comments may only be visible to logged-in visitors. Sign in to view all comments.