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

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

    Yours doesn't work properly since it checks 999*999, then 999*998, then 999*997 until it reaches about 999*583. While it doesn't check 997*995 or something closer to the top which generates a larger number

    function largestPalindrome(){
    
        var arr = [];    
        for(var i =999; i>100; i--){
            for(var j = 999; j>100; j--){
                var mul = j*i;
                if(isPalin(mul)){
                    arr.push(j * i);
                }
            }
        }
    
        return Math.max.apply(Math, arr);
    }
    
    function isPalin(i){
        return i.toString() == i.toString().split("").reverse().join("");
    }
    
    console.log(largestPalindrome());
    

    Here is another approach, store all palindrome generated by 3 numbers in an array, then use Math.max on the array to get the largest palindrome

    0 讨论(0)
  • 2020-12-28 17:50

    instead of creating an Array or ArrayList to store all palindromes, I just created another variable max and stored highest valued palindrome in it.

    My code is in Java, but you can understand the logic from it. Here is my code to better explain what I said (read comments):

    package euler;
    import java.util.ArrayList; import java.util.Collections;
    
    public class Problem4 {
        public static void main (String[] args)
        {
            int product=0;
                int max=0;
            for(int i=999;i>100;i--)
            {
                for (int j=i;j>100;j--)
                {
                    product=i*j;
    
                    if(isPalindrome(product))
                    {
                        //Just store maximum value of product.
                        //Following if loop is required in your code,in place of return i*j;
                                        if(product>max)
                            { max=product; }
                    }
                }
            }
            System.out.println(max);    
        }
        //might be inefficient to create StringBuilder and again String to compare.
        public static boolean isPalindrome(int product)
        {
            boolean isPalindrome=false;
            StringBuilder temp = new StringBuilder(Integer.toString(product)).reverse();
            if(temp.toString().equals(Integer.toString(product)))
            {
                isPalindrome=true;
            }
            return isPalindrome;
        }
    }
    

    What you are doing is returning and breaking out of the loop as soon as you get the first palindrome. Which in your case is not the maximum value palindrome.

    Instead use an if condition and keep a track of maximum values and let the loop continue till end.

    I have added the if condition that lets the loop running and registers the value.

    Got the correct answer from this code.

    PS. Thanks Xan for your input. I guess I could've explained it better first time.

    0 讨论(0)
  • 2020-12-28 17:50
    public static void main(String[] args) {
        int tempAns = 0;
        int max = 999;
        for (int i = 100; i <= max; i++) {
            for (int j = max; j >= i; j--) {
                if (findPalindrome(i * j) && (i * j) > tempAns) {
                    System.out.println("Palindrome: " + j + " * " + i + " = " + j * i);
                    tempAns = i * j;
                }
            }
        }
    }
    
    private static boolean findPalindrome(int n) {
        String nString = String.valueOf(n);
        int j = 0;
        int stringLength = nString.length() - 1;
        for (int i = stringLength; i >= 0; i--) {
    
            if (nString.charAt(j) == nString.charAt(i)) {
    
                if (i == 0) {
                    return true;
                }
                j++;
    
            } else if (nString.charAt(j) != nString.charAt(i)) {
                return false;
            }
    
        }
    
        return false;
    }
    
    0 讨论(0)
提交回复
热议问题