Find the largest palindrome made from the product of two 3-digit numbers. (Java)

后端 未结 4 2061
难免孤独
难免孤独 2021-01-16 20:21

I am having a bit of trouble solving Project Euler question 4. I am inexperienced in programming and didn\'t really understand other answers. I managed to write a code that

4条回答
  •  [愿得一人]
    2021-01-16 21:00

    for 3 digits loop can be started from 100 to 1000(exclusive).

    You can try this ::

    int num1 = 0, num2 = 0;
    for(int i=100; i<1000; i++){
        for(int j=100; j<1000; j++){
            String mul = String.valueOf(i*j);
            if(isPalindrome(mul)){
                num1 = i;
                num2 = j;
            }
        }
    }
    System.out.println(num1*num2);
    

    Implement palindrome method as :

    boolean isPalindrome(String str) {
        String strRev = new StringBuilder(str).reverse().toString();
        return str.equals(strRev);
    }
    

    This works good but take this as reference and improve as needed Thanks

提交回复
热议问题