https://learn.freecodecamp.org/coding-interview-prep/project-euler/problem-4-largest-palindrome-product
My code returns 906609 but FCC says it doesn’t. Anybody know why?
function largestPalindromeProduct(n) {
let largestPalindrome = 0; //tracker
//get starting and ending numbers of n-digit numbers
let lowest = "1";
while (n > 1) {
lowest += "0";
n--;
}
let largest = parseInt(lowest + "0") - 1;
lowest = parseInt(lowest);
for (let i = lowest; i < largest; i++) {
for (let j = lowest + 1; j <= largest; j++) {
let number = i * j;
if (number == number.toString().split("").reverse().join("") ) {
if (number > largestPalindrome) {
largestPalindrome = number;
}
}
}
}
return largestPalindrome;
}
largestPalindromeProduct(3);
Can you show me a console.log() of your output?
I tried it on 2 different platforms and both of them are giving me different output values for n=3.
-
“n: 3”
-
“palindrome: 238832”
-
“n: 3”
-
“palindrome: 299992”
-
“n: 3”
-
“palindrome: 255552”
-
“n: 3”
-
“palindrome: 299992”
-
“n: 3”
-
“palindrome: 222222”
Or you might need to recheck your code.
I think you might be setting off infinite loop protection in the FCC environment and your function terminates before finishing. After so many loops or so much time FCC assumes you have an infinite loop condition and shuts down your function. I get the correct answer outside of FCC but it does take a while
I haven’t done this challenge but by looking over your code and running it I’m thinking you could probably pick up some efficiency by
- starting with the largest numbers and working down - (we can assume that 1XX * 1XX is probably not a pair that is going to yield the largest palindrome for n=3 - 9XX * 9XX is more likely)
- Exiting the loop(s) early when continuing could not possibly yield factors that would make a product larger than the current value of
largestPalindrome
.
- at least checking if
number
was greater than largestPalindrome
before even bothering to check if it was a palindrome. That check for palindrome has to slow things down some.
- maybe a more efficient way to determine
largest
and lowest
. I don’t know if that one will buy you much though - you only do that once.
I get 906609 and only that number which is what FCC is looking for. I’ll try to refactor my code to make it more efficient for the test to pass.
@alhazen1 Thanks for the tips.
- Just by starting with largest number, the test passed even though I did it without a “break” and it would loop through all the numbers it seems. I did went ahead and added a “break” when the loop ran into numbers that would output something less than the current largest Palindrome.
- Further, and what also speed up the code in console, was checking to see if output number was greater than current largest Palindrome before even checking to see if it is a Palindrome.
function largestPalindromeProduct(n) {
let largestPalindrome = 0; //tracker
//get starting and ending numbers of n-digit numbers
let lowest = "1";
while (n > 1) {
lowest += "0";
n--;
}
let largest = parseInt(lowest + "0") - 1;
lowest = parseInt(lowest);
//start with largest number since likely to get to largest palindrome first
for (let i = largest; i >= lowest; i--) {
for (let j = largest - 1; j >= lowest; j--) {
let number = i * j;
//check if number is greater before checking if palindrome for efficiency
if (number > largestPalindrome) {
//check if number is also a palindrome
if (number == number.toString().split("").reverse().join("") ) {
largestPalindrome = number;
}
}
else if (number < largestPalindrome) break;
}
}
return largestPalindrome;
}
largestPalindromeProduct(3);
1 Like