Skip to content

Instantly share code, notes, and snippets.

@jsoffer
Created April 2, 2010 23:05
Show Gist options
  • Save jsoffer/353847 to your computer and use it in GitHub Desktop.
Save jsoffer/353847 to your computer and use it in GitHub Desktop.
import Data.List (sortBy)
data Op = UP | DN deriving (Show, Eq)
serie :: Integer -> [Op]
serie 1 = cycle [UP,DN]
serie x
| odd x = UP : (serie (div ((3*x)+1) 2))
| even x = DN : (serie (div x 2))
serie_a_entero :: [Op] -> Integer
serie_a_entero xs = sum $ zipWith (\ j k -> if j == UP then k else 0) xs (map (2^) [0..])
-- el nivel n en el árbol de congruencias
nivel :: Integer -> [Integer]
nivel n = reordena xs where
xs = 0 : map (serie_a_entero.reverse.(take $ fromInteger n).serie) [1..((2^n)-1)]
reordena ys = map fst $ sortBy (\ j k -> compare (snd j) (snd k)) $ zip [0..] ys
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment