Last active
September 7, 2016 17:58
-
-
Save darkf/56839931856bbfb060cfbd3582e31fee to your computer and use it in GitHub Desktop.
Baseless number encoder
This file contains hidden or 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.Numbers.Primes (primes, primeFactors) | |
import Data.List (elemIndex) | |
import Control.Monad (forM_) | |
data N = P N | Z | Prod [N] deriving (Eq, Show) | |
primes' = 1 : primes | |
primeIndex n = let Just idx = elemIndex n primes' in idx | |
encode :: Int -> N | |
encode 0 = Z | |
encode 1 = P Z | |
encode n = Prod $ map (P . encode . primeIndex) $ primeFactors n | |
decode :: N -> Int | |
decode Z = 0 | |
decode (P n) = primes' !! decode n | |
decode (Prod ns) = product $ map decode ns | |
main = forM_ [0..2^32-1] $ \n -> encode n `seq` return () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment