-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution55.java
More file actions
47 lines (44 loc) · 1.19 KB
/
Copy pathSolution55.java
File metadata and controls
47 lines (44 loc) · 1.19 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
import java.math.BigInteger;
import java.lang.StringBuilder;
public class Solution55{
//Need to adapt this for BigInteger...
/*static int reverse(int n){
int x = 0;
System.out.println("n: " + n);
while(n > 0){
x = x * 10 + n % 10;
n /= 10;
}
System.out.println("reverse: " + x);
return x;
}*/
static String reverse(BigInteger num){
String str = num.toString();
StringBuilder sb = new StringBuilder(str);
return sb.reverse().toString();
}
static boolean isPalindrome(String n){
return n.equals(reverse(new BigInteger(n)));
}
static boolean isLychrel(int n){
BigInteger test = BigInteger.valueOf(n);
for(int i = 0; i < 50; i++){
test = test.add(new BigInteger(reverse(test)));
if(isPalindrome(test.toString())){
return false;
}
}
return true;
}
public static void main(String[] args) {
int i = 0;
int count = 0;
while(i < 10000){
if(isLychrel(i)){
count++;
}
i++;
}
System.out.println(count);
}
}