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

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

    As explained in @VisioN's comment:

    995*583 = 580085 is a palindrome.

    993*913 = 906609 is also a (larger) palindrome.

    Your code checks 995*583 before 993*913 and exits at the first palindrome found, so it doesn't return the largest palindrome.

    Solution: get the largest palindromes starting from 999*999 = 998001 downwards and check if they can be written as xyz*abc.

    Or simply use the accepted solution from the question you linked :). Your solution, but instead of returning when you find the first palindrome, check if it is larger than the largest one already found, in which case you need to replace it. You can stop as soon as the largest palindrome is larger than i*999.

提交回复
热议问题