-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem026.java
More file actions
42 lines (41 loc) · 1.24 KB
/
Problem026.java
File metadata and controls
42 lines (41 loc) · 1.24 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
import java.math.BigDecimal;
import java.math.RoundingMode;
/**
* Finds the longest recurring string of digits in a fraction 1/d, d < 1000
* @author Sean Titus
* @version 1.0
*/
public class Problem026 {
/**
* Finds how many recurring digits a BigDecimal has.
* @param num The BigDecimal being checked for recurring digits
* @return The length of the recurring sequence
*/
private static int recur(BigDecimal num) {
String numStr = num.toPlainString();
int period = 2;
int start = 2;
while(period < (numStr.length() - 1)/2) {
if (numStr.substring(start, start + period)
.equals(numStr.substring(start + period, start + 2 * period))) {
return period;
}
period++;
}
return -1;
}
public static void main(String[] args) {
PrimeGen pg = new PrimeGen(1000);
int longest = 0;
int d = 0;
for (int i : pg.getPrimesList()) {
int r = recur(new BigDecimal(1.0).divide(
new BigDecimal(i), 5000, RoundingMode.HALF_UP));
if (r >= longest) {
longest = r;
d = i;
}
}
System.out.println(d);
}
}