module Exercise_12 where import Data.List import Data.Ord import System.IO import Data.Maybe import Test.QuickCheck isPrime :: Int -> Bool isPrime n | n <= 1 = False | otherwise = primeAux (n-1) where primeAux 1 = True primeAux i = n `mod` i /= 0 && primeAux (i-1) -- returns all primes up to the passed number primes :: Int -> [Int] primes n = [i | i<-[2..n], isPrime i] {-WETT-} {-MCCOMMENT Hey MC, if you dont mind O(n!), this is my token-optimized encrypt function. Suggested activities while this is running are, depending on the input length: -get a cup of coffee (11) -watch the heat death of the unviverse (anything over 24) encrypt' cs = fromJust $ find (\x -> decrypt x == cs) $ permutations cs -} encrypt :: String -> String encrypt cs = map fst (filter (isPrime.snd) xs ++ filter (not.isPrime.snd) xs) where xs = zip cs [1..] decrypt :: String -> String decrypt cs = map fst $ sortOn snd $ zip cs $ primes (length cs) ++ filter (not.isPrime) [1..] {-TTEW-} decrypt' :: String -> String decrypt' cs = map fst $ sortOn snd $ zip cs $ primes l ++ ([1..l] \\ primes l) where l = length cs decrypt'' :: String -> String decrypt'' cs = undefined encrypt' :: String -> String encrypt' cs = fromJust $ find (\x-> decrypt x == cs) $ permutations cs