Project Euler #4: Largest palindrome product

Sort by

recency

|

301 Discussions

|

  • + 0 comments

    C++ code

    #include <cmath>
    #include <cstdio>
    #include <iostream>
    using namespace std;
    
    bool palindrome(int n){
        string s(to_string(n));
        int l= s.length();
        for(int i=0;i<l/2;i++){
            if(s[i] != s[l-i-1]) return false;
        }
        return true;
    }
    
    int find_max(int n){
        int max= 0;
        int produit;
        for(int n1=100;n1<1000;n1++){
            for(int n2=n1;n2<1000;n2++){
                produit=n1*n2;
                if(produit > max && produit < n && palindrome(produit))
                   max=produit;
           }
        }
        return max;
    }
    
    
    int main() {
        int T, N;
        cin >> T;
        for(int i=0; i< T; i++){
            cin >> N;
            cout << find_max(N) << endl;
        }   
        return 0;
    }
    
  • + 0 comments

    def is_palindrome(n): return str(n) == str(n)[::-1] def checker(k): for i in range(999, 99, -1):
    if k % i == 0 and 100 <= k // i <= 999: return True return False

    def main(): t = int(input()) for _ in range(t): n = int(input()) for i in range(n - 1, 101101 - 1, -1): if is_palindrome(i) and checker(i): print(i) break

    if name == "main": main()

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