Project Euler: #4 - Largest palindrome product

Search for a command to run...

No comments yet. Be the first to comment.
In this series, I will be discussing Project Euler Solutions. 'Project Euler' helps in improving problem-solving and programming skills. Remember, there is always a better solution to the problem. 💯
Problem 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? Problem Description The Problem is s...
What is JavaScript? Discover how this quirky yet powerful language runs in browsers, what makes it tick, and how you can start adding it to your sites

Problem If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters w...

Problem 215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. What is the sum of the digits of the number 21000? Problem Description The problem is pretty self-explanatory. 215 is 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26....

Problem Starting in the top left corner of a 2*2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner. How many such routes are there through a 20*20 grid? Problem Description We are given a...

Problem The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even) n → 3n + 1 (n is odd) Using the rule above and starting with 13, we generate the following sequence: 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → ...

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.
A palindromic number reads the same both forward and also backward.
One such palindromic number is 9009 which is the largest palindrome made by multiplying two 2-digit numbers, 91 * 99
We need to find the largest Palindrome that can be made by multiplying two 3-digit numbers
The smallest 3-digit number is 100 and the largest is 999. Since we need the largest palindrome, we approach this from the largest to the smallest, 999 -> 100.
Two reversed for loops starting from 999 for each number, eg: 99 * 99, 99 * 98, 99 * 97 etc
To check if the product is Palindrome,
convert the number to string,
then split it into an array,
reverse the array &
join the items to get the reversed number in string format.
Now convert the reversed number to a number format and compare with the original number.
If both are equal, then it is a palindrome.
const checkPalindrome = (number) => {
let reverse = String(number)
.split('')
.reverse()
.join('');
return Number(reverse) === number;
}
const retLargestPalindrome = () => {
let maxVal = 0;
for (let i = 999; i >= 100; i--) {
for (let j = 999; j >= 100; j--) {
let prod = i * j;
if (checkPalindrome(prod)) {
if (prod > maxVal) maxVal = prod;
// Can also use maxVal = Math.max(maxVal, prod);
break;
}
}
}
return maxVal;
}
console.log(retLargestPalindrome());
My Solution is found in 04
If you have another or a better solution, please leave it in the comments below.
For the other Project Euler Solutions, please follow the series Project Euler Solutions in JS.
Thank you!