Question
A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given:
Where means , and has a 1-digit recurring cycle. It can be seen that has a 6-digit recurring cycle.
Find the value of for which contains the longest recurring cycle in its decimal fraction part.
Haskell
import Data.List (maximumBy)
import Data.Function (on)
cycleLength :: Integer -> Integer
cycleLength n | even n = 0
| n `rem` 5 == 0 = 0
| otherwise = head [p | p <- [1..], (10^p - 1) `rem` n == 0]
main :: IO ()
main = print $ maximumBy (compare `on` cycleLength) [1,3..1000]
$ ghc -O2 -o decimal-cycle decimal-cycle.hs
$ time ./decimal-cycle
real 0m0.262s
user 0m0.262s
sys 0m0.000s
Python
#!/usr/bin/env python
def recurring_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):
if 1 == 10**t % d:
return t
return 0
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])
Ruby
#!/usr/bin/env ruby
puts (0..1000).map { |d|
(1..d).detect(lambda{0}) { |t| (10**t % d) == 1 }
}.each_with_index.max[1]
Rust
fn cycle_length(n: usize) -> usize {
let mut remainder = 10;
let mut seen = vec![0; 10 * n + 1];
for i in 0.. {
if remainder == 0 {
return 0;
} else if seen[remainder] != 0 {
return i - seen[remainder];
}
seen[remainder] = i;
remainder = 10 * (remainder % n);
}
0
}
fn main() {
let max: usize = (1..1000).max_by_key(|&n| cycle_length(n)).unwrap();
println!("{}", max);
}