-- Represent natural numbers via their prime factor decomposition
-- using position for the factors and recursion for powers

-- the primes

primes = sieve [2..]
         where sieve (p:xs) = p : sieve [ x | x <- xs, x `mod` p > 0]


-- find as many factors of head, then recurse on tail til none

facs n p = case n `divMod` p of
             (q,0) -> (n,m+1) where (n,m) = facs q p
             (q,r) -> (n,0)


-- this is an unfold, for what it's worth ...

pfac m = fac m primes
         where fac n (p:ps) = case facs n p of
                     (1,0) -> []
                     (1,k) -> [k]
                     (r,k) -> k : fac r ps

test n = n == product (zipWith (^) primes (pfac n))


-- show as Unicode circles (bullets?) and brackets

shfac 1 = "0" -- "\xE2\x97\x8F"
shfac n = concatMap p (pfac n)
          where p 0 = "0" -- "\xE2\x97\x8F"
                p 1 = "1" -- "\xE2\x97\x8B"
                p n = "(" {-"\xE2\x9F\xA8"-} ++ shfac n ++ ")" {- "\xE2\x9F\xA9" -}

prtfac = putStr . shfac

dump a b = mapM_ p [a..b]
           where p k = putStrLn $ show k ++ '\t' : shfac k



-- (We could also define a graphical "show function" which 
-- draws this out in a tree-like way.)