Project Euler 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."
The code works well enough until trying to find the largest palindrome of two eight digit numbers. I found a similar question in C++, but there are some differences:
- The main function takes two integers to determine the number of digits of the factors used to produce the products. This allows us to find the largest palindrome for a two digit number and a five digit number or two eight digit numbers.
- Two
for
loops then create the multiplicand and multiplier. I think this could be done better. - The outer
for
loop iterates over the multiplicand to decrement it each time the innerfor
loop has completed. - The inner
for
loop iterates over the multiplier so that inside this loop every possible product from the multiplicand and multiplier are evaluated. This is where I think the largest gains from optimization will come from. I don't think that every product needs to be produced or evaluated to find the largest palindrome nor do I think it requires an inner/outer loop to do so. - The product is then turned into a string so that the first and last values can be compared for equality.
- If they are equal the comparison repeats again after removing the compared values until three or less values are left where the first and last values are equal.
- The product is then stored as the largest result if it is greater than the previous result.
- Finally, the result is returned after all the loops have finished.
I am interested in finding ways to optimize this code.
console.log(largestPalindromeOfTwoFactors(8, 8));
function largestPalindromeOfTwoFactors(multiplicand, multiplier{//Accepts the number of digits of the multipland and the number of digits of the multiplier
var multiplicandString = "";
var multiplierString = "";
for(var i = 0; i < multiplicand; i++){
multiplicandString = multiplicandString.concat(""+9); // 9, 99, 999
}
for(var j = 0; j < multiplier; j++){
multiplierString = multiplierString.concat(""+9); // 9, 99, 999
}
var product = 0;
var result = 0;
multiplicand = parseInt(multiplicandString); //999, 998...
for(multiplicand; multiplicand > 0; multiplicand--){ //Loop 999 times
multiplier = parseInt(multiplierString); //999, 998...
for(multiplier; multiplier > 0; multiplier--){//Loop 998001 times
product = multiplicand * multiplier; //999*999,999*998...998*999,998*998...
var stringProduct = (''+product);// 90909
var strLength = stringProduct.length; //Make sure not to modify actual string
for(strLength; strLength >= 1; strLength--){ //For the length of string
if( stringProduct[0] == stringProduct[stringProduct.length-1]) //If 90909: 9==9, 090:0==0, 9:9==9
{
if(stringProduct.length <= 3){//allow for 090
if(product > result){ //if product is larger than existing result replace it with new larger number
result = product;
}
}
stringProduct = stringProduct.slice(1, -1);//take off the first and last numbers
}
}
}
}
return result;
}