Created
April 14, 2017 02:15
-
-
Save LuxXx/1104f585d6d1ccfd7ba35e4c6f46cdaa to your computer and use it in GitHub Desktop.
Project Euler - Problem 4
This file contains 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
package euler; | |
import java.util.Comparator; | |
import java.util.PriorityQueue; | |
public class Pal { | |
public static void main(String[] args) { | |
System.out.println(isPalindrome(1221)); | |
System.out.println(isPalindrome(1234)); | |
System.out.println(isPalindrome(191)); | |
System.out.println(isPalindrome(123)); | |
PriorityQueue<Integer> p = new PriorityQueue<Integer>(new Comparator<Integer>() { | |
@Override | |
public int compare(Integer o1, Integer o2) { | |
if (o1.intValue() == o2.intValue()) return 0; | |
return (o1.intValue() < o2.intValue() ? 1 : -1); | |
}; | |
}); | |
for (int i = 100; i < 1000; i++) { | |
for (int j = 100; j < 1000; j++) { | |
int n = i*j; | |
if (isPalindrome(n)) { | |
p.add(n); | |
} | |
} | |
} | |
System.out.println(p.peek()); | |
} | |
public static boolean isPalindrome(int n) { | |
String s = String.valueOf(n); | |
for (int i = 0; i < s.length() / 2; i++) { | |
if (s.charAt(i) != s.charAt(s.length() - i - 1)) return false; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment