Skip to content

Instantly share code, notes, and snippets.

@tensorpudding
Created February 11, 2010 00:52
Show Gist options
  • Save tensorpudding/301057 to your computer and use it in GitHub Desktop.
Save tensorpudding/301057 to your computer and use it in GitHub Desktop.
module ROT13 ( rot13 ) where
import Data.Char
import Data.List
rot13 :: String -> String
rot13 = map (rotate 13)
rotate :: Int -> Char -> Char
rotate n c
| isUpper c = rotFrom 65
| isLower c = rotFrom 97
| otherwise = c
where
rotFrom m = chr $ mod ((ord c) - (m - n)) 26 + m
--
-- Try that multiplication idea!
--
cipher53 :: Int -> String -> String
cipher53 n = map (c53 n)
decipher53 :: Int -> String -> String
decipher53 n = map (c53 n')
where
n' = case find (\l -> mod (n*l) 53 == 1) [1..52] of
Nothing -> 0
Just x -> x
c53 :: Int -> Char -> Char
c53 n c = ftoc (mod (n*(ctof c)) 53)
ctof :: Char -> Int
ctof c
| isUpper c = (ord c) - 64
| isLower c = (ord c) - 70
| c == ' ' = 0
| otherwise = 0
ftoc :: Int -> Char
ftoc n
| n > 0 && n <= 26 = chr (n + 64)
| n > 26 && n <= 52 = chr (n + 70)
| n == 0 = ' '
| otherwise = ' '
-- rubbish
phi :: Int -> Int
phi n = length . filter (coprime n) $ [1..n-1]
where
coprime a b = gcd a b == 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment