-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem036.java
More file actions
27 lines (27 loc) · 841 Bytes
/
Problem036.java
File metadata and controls
27 lines (27 loc) · 841 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class Problem36 {
private static String getPalindrome(String s) {
String palindrome = "";
for (int i = s.length() - 1; i >= 0; i--) {
palindrome += s.substring(i, i + 1);
}
return palindrome;
}
private static String stripZeros(String s) {
while (s.indexOf("0") == 0) {
s = s.substring(1);
}
return s;
}
public static void main(String[] args) {
int total = 0;
for (int i = 0; i < 1000000; i++) {
if (i == Integer.parseInt(getPalindrome(String.valueOf(i)))) {
String binary = Integer.toBinaryString(i);
if (binary.equals(stripZeros(getPalindrome(binary)))) {
total += i;
}
}
}
System.out.println(total);
}
}