Created
June 3, 2012 15:04
-
-
Save rjkat/2863841 to your computer and use it in GitHub Desktop.
Uses Brent's Algorithm to compute the cycle length and offset of a function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.List | |
alphaWrap :: Char -> Char | |
alphaWrap c | |
| c == 'z' = 'a' | |
| 'a' <= c && c < 'z' = succ c | |
| otherwise = '?' | |
cycleLength :: Eq a => (a -> a) -> a -> Int | |
cycleLength f x = last $ 1 : unfoldr (cycleLength' f) (1, 1, x, f x) | |
cycleLength' f (power, l, tortoise, hare) | |
| tortoise == hare = Nothing | |
| power == l = Just (1, (power * 2, 1, hare, f hare)) | |
| otherwise = Just (l + 1, (power, l + 1, tortoise, f hare)) | |
cycleOffset :: Eq a => (a -> a) -> a -> Int | |
cycleOffset f x = length . takeWhile (uncurry (/=)) $ values | |
where values = iterate (pairMap f) (x, f' x) | |
f' = foldr (.) id (replicate n f) | |
n = cycleLength f x | |
pairMap f (a, b) = (f a, f b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment