Поиск циклов функции
- findCycle :: (Eq a) => (a -> a) -> a -> Int
- findCycle f x0 = countDistance x0 1
- where
- x1 = getCoincidence (f x0) (f (f x0))
- getCoincidence x0 x1 | x0==x1 = x1
- | otherwise = getCoincidence (f x0) (f (f x1))
- countDistance x0 n | x0==x1 = n
- | otherwise = countDistance (f x0) (n+1)
Поиск циклов в последовательности методом Флойда.
Программа-тест
Вывод:
7
Программа-тест
- m :: Int
- m = 441
- gen :: Int -> Int
- gen x = x*x `mod` m
- main :: IO ()
- main = putStrLn $ show $ findCycle gen 2
- findCycle :: (Eq a) => (a -> a) -> a -> Int
- findCycle f x0 = countDistance x0 1
- where
- x1 = getCoincidence (f x0) (f (f x0))
- getCoincidence x0 x1 | x0==x1 = x1
- | otherwise = getCoincidence (f x0) (f (f x1))
- countDistance x0 n | x0==x1 = n
- | otherwise = countDistance (f x0) (n+1)
7