JavaScript Coding Challenge #7
Today we’re going to tackle the next challenge from Project Euler website:
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.
I’m going through the challenge faster than usual because we already solved some challenges in the past and we are more experienced, right? ;)
First, let’s create a function which will check if a number is palindrome:
function isPalindrome(x){
x = x.toString().split(''); // (1)
var len = x.length; // (2) for(var i=0; i<len/2;i++){ // (3)
if(x[i] !== x[len-1-i]){ // (4)
return false;
}
} return true;
}
- Convert the number to an array of characters. For example
999
will become:["9", "9", "9"]
; - Get the length of the array;
- Loop through half the elements from the array;
- If the
i
-th element is not equal t0 thelen-1-i
-th element (the last minusi
-th element) it means that the number is not palindrome, so we returnfalse
. Else the loop continues until it terminates. Then we returntrue
;
Note: This function will also work with strings and arrays!
Awesome! Now that we have this function we can move on to the next step which is looping through all the pairs of 3 digit numbers, multiplying them and checking if the result is palindrome.
We’re also going to store in a variable for the largest palindrome product we have at a certain point:
function isPalindrome(x){
x = x.toString().split('');
var len = x.length; for(var i=0; i<len/2;i++){
if(x[i] !== x[len-1-i]){
return false;
}
} return true;
}function largestPalindrome(){
var largest = 0; for(var i=999; i>=100; i--){
for(var j=999; j>=100; j--){
var mult = i*j; if(isPalindrome(mult) && mult > largest){
largest = mult;
}
}
} return largest;
}
As you might see, this is pretty slow. For testing purposes I added a count that increases on each iteration of the second for loop, and the result was: 810.000
iterations. That’s HUGE!
But we can improve it by breaking out of the second loop if the product of i
and j
is less then the largest
palindrome we already have. That’s because by decreasing j
we will never get a bigger number.
function isPalindrome(x){
x = x.toString().split('');
var len = x.length; for(var i=0; i<len/2;i++){
if(x[i] !== x[len-1-i]){
return false;
}
} return true;
}function largestPalindrome(){
var largest = 0; for(var i=999; i>=100; i--){
for(var j=999; j>=100; j--){
var mult = i*j; if(mult < largest) break; if(isPalindrome(mult) && mult > largest){
largest = mult;
}
}
} return largest;
}
This simple if
statement reduced the count from 810.000
to 9201
. Sweet! :)
Conclusion
This was a fast solution for the Largest Palindrome Product. I’m sure there are ways to improve it even more. If you found a better way, don’t hesitate to post it down below in the comments section.
You can check out some other coding challenges I have here.
If you liked this challenge and you found it useful, I would sincerely appreciate a click on the Recommend button 💚.