-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestPalindromeProduct.java
More file actions
56 lines (54 loc) · 1.99 KB
/
Copy pathLargestPalindromeProduct.java
File metadata and controls
56 lines (54 loc) · 1.99 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.io.*;
import java.util.*;
class LargestPalindromeProduct {
public static void main (String[] args) {
int digits = Integer.parseInt(args[0]);
// both start at largest number with those digits
int max = (int) Math.pow(10, digits) - 1;
int firstMultipleDistance = 0, secondMultipleDistance = 0;
int bottomLimit = (int) Math.pow(10, digits - 1);
boolean palindromeFound = false;
long product = 0;
// distance is the combined difference the two multiples are from the highest
// value
List<Integer> distance = new ArrayList<Integer>();
distance.add(0);
while (max - firstMultipleDistance >= bottomLimit && max - secondMultipleDistance >= bottomLimit) {
int size = distance.size();
// start in the middle and go outward
int i, j, runs;
if (size % 2 == 0) {
i = size / 2 - 1;
j = size / 2;
runs = size / 2;
} else {
i = j = size / 2;
runs = size / 2 + 1;
}
for (int c = 0; c < runs; --i, ++j, ++c) {
firstMultipleDistance = distance.get(i);
secondMultipleDistance = distance.get(j);
product = (long) ((max - firstMultipleDistance) * (max - secondMultipleDistance));
if (isPalindrome(product)) {
palindromeFound = true;
break;
}
}
if (palindromeFound) {
break;
}
distance.add(distance.get(distance.size() - 1) + 1);
}
System.out.println(product);
}
public static boolean isPalindrome(long product) {
String s = String.valueOf(product);
int len = s.length();
for (int i = 0; i < len / 2; ++i) {
if (s.charAt(i) != s.charAt(len - 1 - i)) {
return false;
}
}
return true;
}
}