Skip to content

Instantly share code, notes, and snippets.

@multitudes
Last active May 3, 2020 14:55
Show Gist options
  • Select an option

  • Save multitudes/b425c24883a1507e351ee3498e6eaa63 to your computer and use it in GitHub Desktop.

Select an option

Save multitudes/b425c24883a1507e351ee3498e6eaa63 to your computer and use it in GitHub Desktop.
Project Euler challenge number 4 - my solution in javascript
// Project Euler: Problem 4: 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 n-digit numbers.
function largestPalindromeProduct(n) {
let largest = 0
let inputString = "9".repeat(n)
let inputNumber = +inputString
for (let i = inputNumber; i >= 1; i--){
outerloop:
for (let j = inputNumber; j >= 1; j--){
let product = i * j
// if my product is bigger than my largest number then it is a candidate to check for palidromes
// else I continue looping
if (largest < product){
if (product.toString() === product.toString().split("").reverse().join("")){
console.log("bingo", product, " = ", i," ", j)
largest = i * j
}
// if the product is getting smaller than it is time to continue with the outerloop
} else {
break outerloop
}
}
}
return largest;
}
console.log(largestPalindromeProduct(3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment