Project Euler #4: Largest palindrome product

  • + 0 comments

    public class Solution {

    public static void main(String[] args) {
    
        int l = 0;
        int f1 = 100;
        int limit = 999;
        int f2 =0;
        int smallest = 0;
        int[] arr;
    
        Scanner in = new Scanner(System.in);
        l = in.nextInt();
        arr = new int[l];
    
        for (int i = 0; i < l; i++) {
    

    // Pattern pttr = Pattern.compile("\d\d\d\d\d\d"); // Matcher m = pttr.matcher(String.valueOf()); // if (!m.matches()) { // // continue; // // } else { // // smallest = l; // //System.out.println(i); // //System.out.println(temp/i); // // // }

            arr[i]=in.nextInt();
    
        }
    
        for (int k = 0; k < 2; k++) {
    
            l=arr[k];
    
            while(true) {
    
                if(checkPalindrome(l)) {
    
                    for(int i = f1 ; i<limit ; i++) {
    
                        if(l%i == 0) {
    
    
    
                            int temp = l;
    
    
    
                            Pattern pttr = Pattern.compile("\\d\\d\\d");
                            Matcher m = pttr.matcher(String.valueOf(temp/i));
                            if (!m.matches()) {
    
                                continue;
    
                            } else {
    
                                smallest = l;
                                //System.out.println(i);
                                //System.out.println(temp/i);
    
    
                            }
    
    
                            break;
    
                        }
    
                    }
    
                }
    
                l=l-1;
    
    
    
                if (l<100000) {
    
                    break;
    
    
                }
    
                if (smallest!=0) {
    
                    break;
    
                }
    
            }
    
            System.out.println(smallest);
            smallest = 0;
    
    
    
        }
    
    
    
    
    
    }
    
    
    
    public static boolean checkPalindrome(int n) {
    
        String s = String.valueOf(n);
    
        if(s.charAt(0) == s.charAt(s.length()-1)) {
    
            int r = 0;
            int sum = 0;
            int temp = n;
    
            while (n>0) {
    
                r=n%10;  //getting remainder
                sum=(sum*10)+r;
                n=n/10;
            }
    
            if (temp == sum) {
    
                return true;
            }
    
    
        }
    
        return false;
    
    
    
    }
    

    }