A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given:
1/2 = 0.5
1/3 = 0.(3)
1/4 = 0.25
1/5 = 0.2
1/6 = 0.1(6)
1/7 = 0.(142857)
1/8 = 0.125
1/9 = 0.(1)
1/10 = 0.1
Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. It can be seen that 1/7 has a 6-digit recurring cycle.
1/2 = 0.5
1/3 = 0.(3)
1/4 = 0.25
1/5 = 0.2
1/6 = 0.1(6)
1/7 = 0.(142857)
1/8 = 0.125
1/9 = 0.(1)
1/10 = 0.1
Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. It can be seen that 1/7 has a 6-digit recurring cycle.
Find the value of d 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.
1.package euler;
2.
3.import java.util.HashSet;
4.import java.util.Set;
5.
6./**
7. *
8. * @author hongfa.yy
9. * @version 2013-2-6 下午4:48:51
10. */
11.public class Problem26 {
12. public static void main(String[] args) {
13. int max = 0;
14. int max_d=0;
15. for (int i = 1; i < 1000; i++) {
16. int count = count(i);
17. if (count > max){
18. max_d=i;
19. max = count;
20. }
21.
22. }
23. System.out.println(max_d);
24. }
25.
26. private final static int decimalism = 10;
27.
28. private static int count(int denominator) {
29. Set<Integer> set = new HashSet<>();
30. int mode = 1 * decimalism % denominator;
31. while (mode != 0) {
32. if (!set.contains(mode))
33. set.add(mode);
34. else
35. return set.size();
36. mode = mode * decimalism % denominator;
37. }
38.
39. return 0;
40. }
41.}