We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. All Contests
  2. ProjectEuler+
  3. Project Euler #4: Largest palindrome product
  4. Discussions

Project Euler #4: Largest palindrome product

Problem
Submissions
Leaderboard
Discussions

Sort 260 Discussions, By:

recency

Please Login in order to post a comment

  • rakeshkulungrai
    3 weeks ago+ 0 comments

    Python 3

    import sys
    t = int(input().strip())
    for a0 in range(t):
        n = int(input().strip())
        count =0
        for i in range(n-1,10000,-1):
            if str(i)==str(i)[::-1]:
                for j in range(100,1000):
                    if i%j==0 and i/j <=999:
                        count +=1
                        print(i)
                        break;
            if count ==1:
                break;
    
            
        
    
    0|
    Permalink
  • Donolato
    3 weeks ago+ 0 comments

    Java has a NavigableSet interface (implemented by TreeSet) that is helpful for this situation. So first we construct the set, and then query for the result using the lower method.

    public class Solution {
      
      static boolean isPalindrome(int test) {
        String c = String.valueOf(test);
        return c.equals(new StringBuilder(c).reverse().toString());
      }
      
      static NavigableSet<Integer> createPalindromesSet() {
        NavigableSet<Integer> numbers = new TreeSet<>();
        for(int i = 100;i<1_000;i++){
          for(int j = Math.max(i,100_000/i); j<1_000;j++) {
            int product = i * j;
            if(product%11==0 && isPalindrome(product)) {
              numbers.add(product);;
            }
          }
        }
        return numbers;
      }
    
      public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        NavigableSet<Integer> numbers = createPalindromesSet();
        for(int a0 = 0; a0 < t; a0++){
            int n = in.nextInt();
            System.out.println(numbers.lower(n));
        }
        in.close();
      }
    }
    
    0|
    Permalink
  • pinakapani1806
    3 weeks ago+ 0 comments

    Java Solution;

    import java.util.; import java.text.; import java.math.; import java.util.regex.;

    public class Solution {

    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        for(int a0 = 0; a0 < t; a0++){
            int n = in.nextInt();
    
            System.out.println(largestPalindrome(n));
        }
    }
    
    public static long palindrome(long a){
        long rev = 0;
        while(a!=0){
            rev = rev*10 + a%10;
            a = a/10;
        }
        return rev;
    }
    
    public static boolean check(long a){
        return a == palindrome(a);
    }
    
    public static long largestPalindrome(long n){
            long largest = 0;
            long res=0;
            for(int i=999; i>=100; i--){
                for(int j=999; j>=100; j--){
                    res = j*i;
                    if(check(res) && res<n){
                        largest = Math.max(largest,res);
                    }
                }
            }
        return largest;
    }
    

    }

    0|
    Permalink
  • xpresskaran98
    4 weeks ago+ 0 comments

    Python 3

    #!/bin/python3
    
    import sys
    
    def checkPalindrome(n):
        number_str = str(n)
        length = len(number_str)
        isPalindrome = True
    
        if (length % 2 != 0):
            length -= 1
    
        i = 0
        while i < length / 2:
            if number_str[i] != number_str[len(number_str) - (i + 1)]:
                isPalindrome = False
                break
            i += 1
    
        return isPalindrome
    
    def findProduct(n):
        palindrome = 0
    
        for i in range(101, 1000):
            for j in range(i, 1000):
                product = i * j
                if product >= n or product <= palindrome:
                    continue
                if checkPalindrome(product):
                    palindrome = product
                
        return palindrome
    
    t = int(input().strip())
    for a0 in range(t):
        n = int(input().strip())
        print(findProduct(n))
    
    0|
    Permalink
  • macdonald_l_kyn
    1 month ago+ 0 comments

    Python3 Solution

    pl = []
    for i in range(100, 1000):
        for j in range(100, 1000):
            pro = i * j
            if str(pro)[::-1] == str(pro):
                pl.append(pro)
    pl.sort(reverse=True)
    for i in range(int(input())):
        n = int(input())
        for j in pl:
            if j < n:
                print(j)
                break
    
    -2|
    Permalink
Load more conversations

Need Help?


View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy