Where $0.1\overline{6}$ means $0.1666…$, and has a 1-digit recurring cycle. It can be seen that $\frac{1}{7}$ has a 6-digit recurring cycle.
Find the value of $d < 1000$ for which $\frac{1}{d}$ contains the longest recurring cycle in its decimal fraction part.
Answer
1
983
Python
1 2 3 4 5 6 7 8 9 10 11
#!/usr/bin/env python defrecurring_cycle(n, d): # solve 10^s % d == 10^(s+t) % d # where t is length and s is start for t in range(1, d): if1 == 10**t % d: return t return0
longest = max(recurring_cycle(1, i) for i in range(2,1001)) print([i for i in range(2,1001) if recurring_cycle(1, i) == longest][0])
publicfinalclassp026implementsEulerSolution{ publicstaticvoidmain(String[] args){ System.out.println(new p026().run()); } public String run(){ int bestNumber = 0; int bestLength = 0; for (int i = 1; i <= 1000; i++) { int len = getCycleLength(i); if (len > bestLength) { bestNumber = i; bestLength = len; } } return Integer.toString(bestNumber); } privatestaticintgetCycleLength(int n){ Map<Integer,Integer> stateToIter = new HashMap<>(); int state = 1; for (int iter = 0; ; iter++) { if (stateToIter.containsKey(state)) return iter - stateToIter.get(state); else { stateToIter.put(state, iter); state = state * 10 % n; } } } }