Thursday, April 29, 2010

[fngeoazc] Encoding Data as a Number

859353774693438282918902971656242164313590376690309509345424195748793272311951

Encoding a list of nonnegative integers as a product of nondecreasing primes, with each integer encoded as the first difference of the indices (in the sequence of all primes) of the prime factors.

The implementation below demonstrates the leading edge trailing edge eratosthenes sieve.

module Main where{ import Char; import Monad; type Prime = Integer; isPrime :: Prime -> Bool; isPrime x = and (map (notDivisible x) (takeWhile (lessThanSquare x) smallPrimes)); lessThanSquare :: Prime -> Prime -> Bool; lessThanSquare s x = x*x<=s; notDivisible :: Prime -> Prime -> Bool; notDivisible big small = (mod big small) /= 0; smallPrimes :: [Prime]; smallPrimes = 3:(filter isPrime [5,7..]); primes=2:(filter isPrime [3,5..]); sequenceDropper :: [Int] -> [a] -> [a]; sequenceDropper [] _ = []; sequenceDropper (i:is) xs = let { newxs = drop i xs; } in (head newxs):(sequenceDropper is newxs); charNumbers :: String -> [Int]; charNumbers s = do{ c<-s; guard(c>='a'); guard(c<='z'); return ((ord c)-(ord 'a')); }; encode :: String -> Prime; encode s = product $ sequenceDropper (charNumbers s) primes; main :: IO (); main = getContents >>= (print . encode); }

Causing the numbers to be small would be useful: perhaps Burrows Wheeler followed by Move To Front.

No comments :