Skip to content

Solution of problem 0004 added. #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions problem0004.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// problem004.js

// 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.

///////////////////////////////////////////////////////////////////////////////
// NOTES ABOUT THE SOLUTION:
// This solution cycles to test all pairs of factors between 111 and 999 that meet the condition of generating a palindrome and saves the largest found.
// I think there must be another optimal solution to avoid testing all cases
// cutting the loop around the largest factor pair
// That's why I thought about doing the loop from highest to lowest.
///////////////////////////////////////////////////////////////////////////////

let bottom = 111
let top = 999

function isPalindrome(n) {
// console.log(`${ n.toString().split("").reverse().join("") }`);
// console.log(`${ n.toString()}`) ;

return n.toString().split("").reverse().join("") === n.toString();
}

// Test isPalindrome condition
// console.log(isPalindrome('1000'));
// console.log(isPalindrome('1001'));
// console.log(isPalindrome('3443'));
// console.log(isPalindrome('3435'));


let i;
let j;
let foundi;
let foundj;
let foundPalindrome;

// Find all cases
i = top;
do {

j = top;
do {

if( isPalindrome( j * i ) )
{
console.log(`FOUND: ${i} x ${j} = ${j * i} is Palindrome`);

if(!foundPalindrome || (i*j) > foundPalindrome)
{
foundi = i;
foundj = j;
foundPalindrome = (i*j);
}

} else {
// console.log(`FOUND: ${i} x ${j} = ${j * i} is NOT Palindrome`);
}

j--;
}
while ( j >= bottom /*&& !(found1 && found2)*/)


i--;
}
while ( i >= bottom /* && !(found1 && found2) */ )

console.log(`Largest Palindrome => ${foundi} 𝗑 ${foundj} = ${foundPalindrome}`);