исходная задача с Эйлера https://projecteuler.net/problem=26
Выдает
[(2,"0.5(0)"),
(3,"0.(3)"),
(4,"0.25(0)"),
(5,"0.2(0)"),
(6,"0.1(6)"),
(7,"0.(142857)"),
(8,"0.125(0)"),
(9,"0.(1)"),
(10,"0.1(0)")]
....
[(100,"0.01(0)"),
(101,"0.(0099)"),
(102,"0.0(0980392156862745)"),
(103,"0.(0097087378640776699029126213592233)"),
(104,"0.009(615384)"),
...
(113,"0.(0088495575221238938053097345132743362831858407079646017699115044247787610619469026548672566371681415929203539823)"),
...
Но народ на Эйлере - монстры, они решили там задачу 26 еще радикально быстрее, даже не вычисляя сами по себе циклы, а использую математическое знание:
http://mathworld.wolfram.com/FullReptendPrime.html
the period of a number n's reciprocal is the smallest k such that n divides 10^k - 1.
так что у монстров решение такое
На то они и монстры!
import Data.List findCycle xs = (take firstOccurenceInd tmp, drop firstOccurenceInd tmp) where repeatingElement = xs !! (length tmp) tmp = last $ takeWhile (check . reverse) (inits xs) firstOccurenceInd = length $ takeWhile (\x -> x /= repeatingElement) xs check [] = True check [x] = True check (x:xs) = if x `elem` xs then False else True process n = (n, "0." ++ (tail prefixPart) ++ cyclicPart) where prefixPart = concat $ map (\x -> show $ fst x) (fst $ cycleInfo) cyclicPart = "(" ++ (concat $ map (\x -> show $ fst x) (snd $ cycleInfo)) ++ ")" cycleInfo = findCycle $ iterate divisionStep (0,1) divisionStep (_,x) = ( 10*x `quot` n, 10*x `mod` n) main = do print $ map process [2..10] print $ map process [100..120]
Выдает
[(2,"0.5(0)"),
(3,"0.(3)"),
(4,"0.25(0)"),
(5,"0.2(0)"),
(6,"0.1(6)"),
(7,"0.(142857)"),
(8,"0.125(0)"),
(9,"0.(1)"),
(10,"0.1(0)")]
....
[(100,"0.01(0)"),
(101,"0.(0099)"),
(102,"0.0(0980392156862745)"),
(103,"0.(0097087378640776699029126213592233)"),
(104,"0.009(615384)"),
...
(113,"0.(0088495575221238938053097345132743362831858407079646017699115044247787610619469026548672566371681415929203539823)"),
...
Но народ на Эйлере - монстры, они решили там задачу 26 еще радикально быстрее, даже не вычисляя сами по себе циклы, а использую математическое знание:
http://mathworld.wolfram.com/FullReptendPrime.html
the period of a number n's reciprocal is the smallest k such that n divides 10^k - 1.
так что у монстров решение такое
import Data.List nums = [ n | n <- [3,5..], n `mod` 5 /= 0 ] period n = head $ [ p | p <- [1..], (10^p - 1) `mod` n == 0 ] answer = fst $ maximumBy (\(_,a) (_,b) -> compare a b) $ map (\n -> (n,period n)) $ takeWhile (<1000) nums main = print answer
На то они и монстры!