-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem26.java
62 lines (53 loc) · 1.62 KB
/
Problem26.java
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.javamultiplex.projecteuler;
import java.util.LinkedHashMap;
import java.util.Map;
public class Problem26 {
public static void main(String[] args) {
int n = 1, target = 1000;
String result = null;
int length = 0, max = 0, denominator = 0;
for (int d = 2; d < target; d++) {
result = getRecurringDecimalDigits(n, d);
length = result.length();
if (length > max) {
max = length;
denominator = d;
}
}
System.out.println("The value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part : "
+ denominator);
}
private static String getRecurringDecimalDigits(int n, int d) {
/*
* In map, we will store remainder as a key and its position as a value.
*/
Map<Integer, Integer> map = new LinkedHashMap<>();
String result = "";
// Get first remainder.
int remainder = n % d;
int position = 0, temp = 0;
/*
* Run while loop until remainder !=0 and map doesn't contains
* remainder.
*/
while (remainder != 0 && !(map.containsKey(remainder))) {
position = result.length();
map.put(remainder, position);
remainder = remainder * 10;
temp = remainder / d;
// Convert int to String and append into result.
result += String.valueOf(temp);
remainder = remainder % d;
}
if (remainder == 0) {
return "";
} else {
/*
* As you know we are storing remainder and its position in map. and
* our result contains recurring sequence. So extracting the
* recurring sequence from result.
*/
return result.substring(map.get(remainder));
}
}
}