Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Largest palindrome product in Javascript

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 made this code to find the solution but the answer in Project Euler website still incorrect:

function Palindromic(x) {
    var pal = parseInt(x.toString().split('').reverse().join(''));

    if (pal === x)
        return true;
    else
        return false;
}

var x = 100,
    y = 100,
    product = x * y;

for (x; x <= 999; x++) {
    for (y = x; y <= 999; y++) {
        product = x * y;
        if (Palindromic(product)) {
            console.log(x + '*' + y + '=' + product);
        }
    }
}

Is there a problem in my code?! anyway, the answer that i got was 888888 from 924*962

like image 347
elkebirmed Avatar asked Sep 17 '13 06:09

elkebirmed


People also ask

What is the biggest palindrome number?

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 202, … (sequence A002113 in the OEIS). Palindromic numbers receive most attention in the realm of recreational mathematics.

What is the largest palindrome product of 3-digit numbers?

The largest palindrome made from the product of two 3-digit numbers is 913 * 993 = 906609. Flowchart: There was a problem connecting to the server.

How many 2 digit palindromes are there?

Now palindromic numbers which reads the same if read from backwards also. So such numbers are 11, 22, 33, 44, 55, 66, 77, 88 and 99. So there are 9 palindromic two digit numbers from 1 to 1000.

How many 6 digit palindromes are there?

You can conclude that there are 900 palindromes withfive and 900 palindromes with six digits.


1 Answers

I don't think, there is a real problem with your code. You just do not filter for the largest product, which is not necessarily your last output. Just add an additional check for the largest product, e.g. like this:

var x, y, product, max = 0;

for (x = 100; x <= 999; x++) {
    for (y = x; y <= 999; y++) {
        product = x * y;
        if (Palindromic(product)) {
          if( max < product ) { // this is new
            max = product;
            console.log(x + '*' + y + '=' + product);
          }
        }
    }
}

This returns

913*993=906609

as the largest result.

like image 143
Sirko Avatar answered Sep 20 '22 02:09

Sirko