**Problem 4**

Forth problem from project Euler.

I'm not sure I like my answer to this just yet, but it runs in under a second so it can't be that bad. Feedback on better ways to do these are most welcome!

**Question:**

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

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

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

Answer:

import java.util.SortedSet; import java.util.TreeSet; public class problem4 { public static void main(String[] args) { problem4 p = new problem4(); SortedSet set = new TreeSet(); for(int i = 100; i < 999; i++){ for(int j = 100; j < 999; j++){ if(p.isPalendrome(j * i)){ set.add(j * i); } } } System.out.println(set.last()); } boolean isPalendrome(int num){ String number = String.valueOf(num); StringBuilder sb = new StringBuilder(); for(int i = (number.length() - 1); i >= 0; i--){ sb.append(number.charAt(i)); } if(sb.toString().equals(number)){ return true; } return false; } }

## No comments:

## Post a Comment