Project Euler #4: Largest palindrome product

Sort by

recency

|

299 Discussions

|

  • + 0 comments
    public static void main(String[] args) {
    
            List<Integer> list=new ArrayList<>();
            String x;
            StringBuilder sb;
            int ori;
            for(int i=999;i>=100;i--){
                for(int j=999;j>=100;j--){
                    ori=i*j;
                    x=String.valueOf(ori);
                    sb=new StringBuilder(x);
                    sb=sb.reverse();
                    if(x.equals(sb.toString())){
                        list.add(ori);
                    }
                }
            }
            Collections.sort(list,Collections.reverseOrder());
    
    
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        for(int a0 = 0; a0 < t; a0++){
            int n = in.nextInt();
            for(int i=0;i<list.size();i++){
                if(list.get(i)<n){
                    System.out.println(list.get(i));
                    break;
                }
            }
        }
    

    }

  • + 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;
    
    
    
    }
    

    }

  • + 0 comments
    //My code by C++,solving by the solution divide and conquer
    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    
    bool palindrome(long long n) {
        long long first_num = n;
        long long reverse = 0;
    
        while (n > 0) {
            long long num = n % 10;
            reverse = reverse * 10 + num;
            n /= 10;
        }
    
        return (first_num == reverse);
    }
    bool checker(int k){
        for(int i = 999; i >= 100; i--){
            if(k % i == 0 && k/i >= 100 && k/i <= 999){
                return true;
            }
        }
        return false;
    }
    signed main() {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */
        long long t;
        cin>>t;
        while(t--){
           long long n;
           cin>>n;
           for(long long i = n - 1; i >= 101101; i--){
               if(palindrome(i) == true && checker(i) == true) {
                    cout<<i<<endl;
                    i = INT8_MIN; //get out the "for loop"  when find i 
                }  
           } 
        }
        return (0^0);
    }
    
  • + 0 comments
    import java.io.*;
    import java.util.*;
    
    
    public class Solution {
        public static void main(String[] args) throws IOException {
            Scanner s=new Scanner(System.in);
            int t=s.nextInt();
            while(t-->0)
            {
                int n=s.nextInt();
                System.out.println(lar_Pal_lessthan_n(n));
            }
            s.close();
        }
            public static int lar_Pal_lessthan_n(int n)
            {
                    for(int i= n-1;i>=101101;i--)
                    {
                        if(ispal(i) && t2_t3fact(i) )
                        {
                            return i;
                        }
                    }
                return -1;
            }
            
            public static boolean ispal(int n)
            {
                int org=n;
                int r=0;
                while(n!=0)
                {
                     r=r*10+n%10;
                    n/=10;   
                }
                return org==r;
            }
            public static boolean t2_t3fact(int n)
            {
               for(int i=999;i>=100;i--)
               {
                   if(n%i==0 && n/i>=100 && n/i<=999)
                   {
                   return true;
                   }
               }
                  return false;      
                
            }
    }
    

    Happy Learning!

  • + 0 comments

    t = int(input().strip()) for a0 in range(t): n = int(input().strip()) palindrome = 0

    while palindrome==0:
        n -=1
        if str(n)==str(n)[::-1]:
            for j in range(100, 999):
                f = n/j
                if f%1 == 0 and f <= 999:
                    f= int(f)
                    palindrome=n
                    break
    
    print(palindrome)#, "\n factors:", j, "X", f)