Saturday, January 14, 2012

Problem 4

Problem Statement :Problem4

Algorithm:
We brute over all possible palindromes from 999*999 to 100*100. Then we try to find all possible palindromes and find the largest among them.

Solution:
    public class problem4 {
    
     public static boolean isPalind(int n) {
      String num = n + "";
      for (int j = 0; j < num.length(); j++) {
       if (num.charAt(j) != num.charAt(num.length() - 1 - j))
        return false;
      }
      return true;
     }
    
     public static void main(String args[]) {
      int max=Integer.MIN_VALUE; 
      for (int i = 999; i >= 100; i--) {
       for (int j = i; j >= 100; j--) {
        int prod = i * j;
        if (isPalind(prod)) {
         if(max<prod)
          max=prod;
        }
       }
      }
      System.out.println(max);
     }
    }
    
    

No comments:

Post a Comment