Skip to content

Instantly share code, notes, and snippets.

@simonewebdesign
Last active December 23, 2015 16:49
Show Gist options
  • Select an option

  • Save simonewebdesign/6664293 to your computer and use it in GitHub Desktop.

Select an option

Save simonewebdesign/6664293 to your computer and use it in GitHub Desktop.
import static java.lang.System.out;
import java.util.ArrayList;
import java.util.List;
public class Euler4
{
static final int MIN = 100;
static final int MAX = 999;
public static List<Integer> getPalindromes(int min, int max)
{
List<Integer> palindromes = new ArrayList<>();
for (int i=max; i >= min; i--)
{
for (int j=max; j >= min; j--)
{
int product = i * j;
if (isPalindrome(new Integer(product).toString()))
palindromes.add(product);
}
}
return palindromes;
}
public static boolean isPalindrome(String str)
{
if (str.length() < 0) return false;
if (str.length() <= 1) return true;
char firstChar = str.charAt(0);
char lastChar = str.charAt(str.length()-1);
if (firstChar == lastChar) {
return isPalindrome(str.substring(1, str.length()-1));
}
return false;
}
public static int getHighestNumber(List<Integer> numbers)
{
int highestNumber = -1;
for (int number : numbers)
if (number > highestNumber)
highestNumber = number;
return highestNumber;
}
public static void main(String[] args)
{
List<Integer> palindromes = getPalindromes(MIN, MAX);
out.println(getHighestNumber(palindromes));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment