Posted By

binaryadder on 10/15/11


Tagged


Versions (?)

Problem 4


 / Published in: Java
 

Project Euler Question 4:

"A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 x 99.

Find the largest palindrome made from the product of two 3-digit numbers."

Output:

906609

  1. import java.util.*;
  2.  
  3. public class Prob4{
  4. public static void main(String[] argsv){
  5. System.out.println(largestPalindrome(100,1000));
  6. }
  7.  
  8.  
  9. // Searches for the largest numerical palindrone in the interval [start, end)
  10. private static int largestPalindrome(int start, int end){
  11. int high = 0;
  12. for(int i = start; i < end; i++){
  13. for(int j = start; j < end; j++){
  14. if(isPalindrome(i * j) && ((i * j) > high)){
  15. high = i * j;
  16. }
  17. }
  18. }
  19. return high;
  20. }
  21.  
  22. private static boolean isPalindrome(Integer n){
  23. String s = n.toString();
  24. return s.contentEquals(new StringBuffer(s).reverse());
  25. }
  26. }

Report this snippet  

You need to login to post a comment.