Find the largest palindrome made from the product of two 3-digit numbers - Javascript

前端 未结 21 1503
感情败类
感情败类 2020-12-28 17:16

Can anyone tell me what\'s wrong with the code. Find the largest palindrome made from the product of two 3-digit numbers.

function largestPalind         


        
21条回答
  •  滥情空心
    2020-12-28 17:42

    The above solution will work perfectly fine but we will have issue ONLY when we try to find-out what are those 2 numbers (i = 913 and j = 993)

    I will just modify the solution proposed by Azder

    int max = 0;
    int no1 = 0;
    int no2 = 0;
    
    // not using i >= 100 since 100*100 is not palindrome! :)
    for (var i = 999; i > 100; i--) {
        // because i * j === j * i, no need of both i and j
        // to count down from 999
        for (var j = i; j > 100; j--) {
            var mul = j * i;
            if (isPalin(mul)) {
                if ((i+j) > max) {
                   max = i+j;
                   no1 = i; no2 = j;
                }
            }
        }
    }
    
    //Now we can get the 2 numbers (no1=993 and no2=913)
    
    return (no1*no2);
    

提交回复
热议问题