Last active
December 23, 2015 16:49
-
-
Save simonewebdesign/6664293 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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