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

前端 未结 21 1498
感情败类
感情败类 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:25

    Suggesting a solution using underscore.js. First, find all palindromes and then loop through them starting from the largest one and return the one which has two 3-digit prime factors.

    function isPalindrome(num) {
        var str = num.toString();
        return str.split('').reverse().join('') === str;
    }
    
    function palindromes() {
        var max = 999 * 999;
        var min = 100 * 100;
        return _.select(_.range(max, min, -1), isPalindrome);
    }
    
    palindromes().find(function (x) {
        if (_.find(_.range(999, 100, -1), function (y) {
            return (x % y === 0 && y != x / y && x / y < 1000) ? true : false;
        })) return true;
    })
    

提交回复
热议问题