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

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

    #define MAX(a, b) ((a) > (b) ? (a) : (b))
    int largestPalindrome()
    {
        int ret = 0;
        for (int i = 999; i > 100; --i)
        {
            int jLimit = MAX(ret / i, 100);
            for (int j = i; j > jLimit; --j)
            {
                int ans = i * j;
                if (isPalin(ans))
                {
                    ret = MAX(ans, ret);
                }
            }
        }
    
        return ret;
    }
    

    Reasons explained above.

    We can recompute the range of j when we find a palindrome product.This should be faster.

提交回复
热议问题