-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProblem_055.java
More file actions
45 lines (34 loc) · 1.1 KB
/
Problem_055.java
File metadata and controls
45 lines (34 loc) · 1.1 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
package euler;
import java.math.BigInteger;
public class Problem_055 {
static int lychrelNums() {
return lychrelNums(10000);
}
private static int lychrelNums(int thresh) {
int count = 0;
for (int i = 10; i < thresh; i++) {
if (isLychrel(i)) count++;
}
return count;
}
private static boolean isLychrel(int num) {
int i = 0;
BigInteger bi = new BigInteger(Integer.toString(num));
while (i < 50) {
bi = bi.add(reverse(bi));
if (isPalindrome(bi)) return false;
i++;
}
return true;
}
private static BigInteger reverse(BigInteger bi) {
String numStr = bi.toString();
StringBuilder reverse = new StringBuilder(numStr).reverse();
return new BigInteger(reverse.toString());
}
private static boolean isPalindrome(BigInteger bi) {
String numStr = bi.toString();
StringBuilder reverse = new StringBuilder(numStr).reverse();
return numStr.equals(reverse.toString());
}
}