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

后端 未结 4 2034
难免孤独
难免孤独 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 20:44

    Here is my take on the same question...

    It is way more easier to create a list, that we know will be filled from smallest to the largest, because counter numbers only rize...

            List<Integer> palindrome = new ArrayList<>(); // create integer list
    
        for ( int i = 900; i<999; i++){
            for ( int j = 900; j < 950; j++){
                // both counter numbers are bigger than 900
                // because there will those two int's be, logically
                String newMul = String.valueOf(i * j);  // make it string for palindrome comparation
                String strRev = new StringBuilder(newMul).reverse().toString(); // rotate previous string
                                                                                // for palindrome comparation 
                if(newMul.equals(strRev)){  //if its equal
                palindrome.add(i * j);      //will put multiplication result of i and j into list
                }
            }
        }
        System.out.println(palindrome.get(palindrome.size()-1)); // last number in list is the 
    }                                                            // biggest so lets print it out
    

    Basicly, you just need little casting to se if it is a palindrome, but if it is, just multiply i and j to put them into list... It will loop for a long time, but that way you checked all the numbers bigger than 900 for the biggest palindrome...

    Hope I helped...

    0 讨论(0)
  • 2021-01-16 20:49

    Here is a code to check PALINDROME-ness of a number without converting it to a String.

    bool checkPalindrome(int n)
    {
        int num = n;
        int s = 0;
        while(num!=0)
        {
          s = s*10 + (num%10);
          num = num/10;
        }
        if(s==n)
          return true;
        return false;
    }
    

    As you can see that if n = 120, the reverse is calculated as s = 21 instead of 021, but that is fine, keeping in mind that a palindrome number won't end in 0, because if it does, then it would have to begin with 0 which makes it an invalid number!!!!

    Hope this helps!!!

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-01-16 21:07

    Here is a method which loops over all numbers from 1-999 and multiplies them with all possible numbers from 1-999. You will need to define a method isPalindrome(int) that will check if a given int is a palindrome.

    //save the largest number
    int largest = 0;
    
    //loop over every possible product of numbers from 100-999
    for (int i = 999; i >= 100; i--) {
     for (int j = i; j >= 100; j--) {
      int curr = i * j;
    
      //if the current number is a palindrome and is greater than the last found largest
      if (isPalindrome(curr) && curr > largest) {
    
       //save it as the new largest found number
       largest = curr;
      }
     }
    }
    

    The isPalindrome method might look like this:

    private static boolean isPalindrome(Integer possible) {
        String toStr = possible.toString();
        int len = toStr.length();
        if (len % 2 == 1) {
            len = len - 1;
        }
    
        for (int i = 0; i < len / 2; i++) {
            if (toStr.charAt(i) != toStr.charAt(toStr.length() - (1 + i))) {
                return false;
            }
        }
    
        return true;
    }
    
    0 讨论(0)
提交回复
热议问题