-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathProblem_55.java
More file actions
38 lines (35 loc) · 824 Bytes
/
Problem_55.java
File metadata and controls
38 lines (35 loc) · 824 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
28
29
30
31
32
33
34
35
36
37
38
import java.math.BigInteger;
class Number {
public static void main(String[] args) {
long count = 0;
for (long i = 1; i < 10000; i++) {
if (isLychrelNumber(i)) {
count++;
}
}
System.out.println(count);
}
private static boolean isLychrelNumber(long number) {
BigInteger next = BigInteger.valueOf(number);
int numIterations = 1;
do {
next = next.add(new BigInteger(
new StringBuffer(next.toString()).reverse().toString()));
if (isPalindrome(next.toString())) {
return false;
}
numIterations++;
}
while (numIterations <= 50);
return true;
}
private static boolean isPalindrome(String word) {
int length = word.length();
for (int i = 0; i < length/2; i++) {
if (word.charAt(i) != word.charAt(length-1-i)) {
return false;
}
}
return true;
}
}