Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 92423e6

Browse files
author
Ram swaroop
committed
initial commit
1 parent 67bed69 commit 92423e6

File tree

2 files changed

+174
-0
lines changed

2 files changed

+174
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package me.ramswaroop.misc;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileReader;
5+
import java.io.IOException;
6+
7+
/**
8+
* Created by IntelliJ IDEA.
9+
*
10+
* @author: ramswaroop
11+
* @date: 9/16/15
12+
* @time: 10:53 PM
13+
*/
14+
public class ReverseAndAdd {
15+
16+
public static long getReverse(long n) {
17+
return Long.valueOf(new StringBuilder().append(n).reverse().toString());
18+
}
19+
20+
public static boolean isPalindrome(long n) {
21+
return n == getReverse(n);
22+
}
23+
24+
public static boolean isNegative(long n) {
25+
return n < 0;
26+
}
27+
28+
public static long[] reverseAddAndCheck(String n) {
29+
long additions = 0;
30+
long original = Long.valueOf(n);
31+
32+
boolean isNegative = isNegative(original);
33+
if (isNegative) original = -original;
34+
35+
long reverse;
36+
37+
while (!isPalindrome(original + (reverse = getReverse(original)))) {
38+
original += reverse;
39+
additions++;
40+
}
41+
42+
original += reverse;
43+
if (isNegative) original = -original;
44+
45+
return new long[]{additions, original};
46+
}
47+
48+
public static void readFile(String filePath) {
49+
50+
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
51+
52+
String line;
53+
54+
while ((line = br.readLine()) != null) {
55+
long[] result = reverseAddAndCheck(line);
56+
System.out.println(result[0] + " " + result[1]);
57+
}
58+
59+
} catch (IOException e) {
60+
e.printStackTrace();
61+
}
62+
}
63+
64+
public static void main(String a[]) {
65+
readFile(a[0]);
66+
System.exit(0);
67+
}
68+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
1
2+
5
3+
59
4+
69
5+
79
6+
89
7+
166
8+
188
9+
193
10+
829
11+
167
12+
849
13+
177
14+
999
15+
739
16+
989
17+
869
18+
187
19+
1397
20+
2069
21+
1797
22+
1798
23+
6999
24+
1297
25+
10797
26+
10853
27+
10921
28+
10971
29+
13297
30+
10548
31+
13293
32+
17793
33+
20889
34+
80359
35+
13697
36+
10794
37+
15891
38+
70759
39+
70269
40+
10677
41+
10833
42+
10911
43+
700269
44+
106977
45+
108933
46+
600259
47+
131996
48+
600279
49+
141996
50+
600579
51+
147996
52+
178992
53+
190890
54+
600589
55+
150296
56+
1009227
57+
1007619
58+
1009246
59+
1008628
60+
1007377
61+
1001699
62+
1009150
63+
1058921
64+
1050995
65+
1003569
66+
1036974
67+
1490991
68+
3009179
69+
1008595
70+
1064912
71+
1998999
72+
7008429
73+
1000689
74+
1005744
75+
1007601
76+
7008899
77+
9008299
78+
10905963
79+
10069785
80+
10089342
81+
11979990
82+
10029372
83+
10029826
84+
16207990
85+
90000589
86+
10309988
87+
100389898
88+
100055896
89+
110909992
90+
160009490
91+
800067199
92+
151033997
93+
100093573
94+
103249931
95+
107025910
96+
180005498
97+
100239862
98+
140669390
99+
1090001921
100+
7007009909
101+
1009049407
102+
9000046899
103+
1050027948
104+
1304199693
105+
5020089949
106+
1005499526

0 commit comments

Comments
 (0)