Problem 26

http://projecteuler.net/index.php?section=problems&id=26
1/dが循環少数のとき、d < 1000 の範囲で循環の長さが最大になるのは?

import Data.List (unfoldr, maximumBy)
import Data.IntSet (empty, member, insert)

main = print $ fst $ maximumBy (\(_, len0) (_, len1) -> len0 `compare` len1) $ map (\i -> (i, cycleLength 1 i)) [1..999]

cycleLength x y = cycleLength' empty [] $ divmods x y
    where cycleLength' _ _ [] = 0
          cycleLength' set history (divmod@(_, mod):dms) | member mod set = countLength 1 mod history
                                                         | otherwise      = cycleLength' (insert mod set) (mod:history) dms
          countLength i mod (x:xs) | mod == x  = i
                                   | otherwise = countLength (i+1) mod xs

divmods x y = unfoldr f x
    where f x = case divMod x y of
                (_, 0)          -> Nothing
                divmod@(_, mod) -> Just (divmod, mod * 10)

1/983が、長さ982の循環らしい。
0.000010172939979654120040691759918616480162767039674465920
65106815869786368260427263479145473041709053916581892166836
21566632756866734486266531027466937945066124109867751780264
49643947100712105798575788402848423194303153611393692777212
61444557477110885045778229908443540183112919633774160732451
67853509664292980671414038657171922685656154628687690742624
61851475076297049847405900305188199389623601220752797558494
40488301119023397761953204476093591047812817904374364191251
27161749745676500508646998982706002034587995930824008138351
98372329603255340793489318413021363173957273652085452695829
09460834181078331637843336724313326551373346897253306205493
38758901322482197355035605289928789420142421159715157680569
68463886063072227873855544252288911495422177009155645981688
70803662258392675483214649033570701932858596134282807731434
38453713123092573753814852492370295015259409969481180061037
63987792472024415055951169888097660223804679552390640895218
71820956256358087487283825025432349949135300101729399796541
20040691759918616480162767039674465920651068158697863682604
27263479145473041709053916581892166836215666327568667344862
66531027466937945066124109867751780264496439471007121057985
75788402848423194303153611393692777212614445574771108850457
78229908443540183112919633774160732451678535096642929806714
14038657171922685656154628687690742624618514750762970498474
05900305188199389623601220752797558494404883011190233977619
53204476093591047812817904374364191251271617497456765005086
46998982706002034587995930824008138351983723296032553407934
89318413021363173957273652085452695829094608341810783316378
43336724313326551373346897253306205493387589013224821973550
35605289928789420142421159715157680569684638860630722278738
55544252288911495422177009155645981688708036622583926754832
14649033570701932858596134282807731434384537131230925737538
14852492370295015259409969481180061037639877924720244150559
51169888097660223804679552390640895218718209562563580874872
83825025432349949135300101729399796541200406917599186164