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 :)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.