Skip to content

Instantly share code, notes, and snippets.

@jsoffer
Created January 19, 2011 17:23
Show Gist options
  • Save jsoffer/786484 to your computer and use it in GitHub Desktop.
Save jsoffer/786484 to your computer and use it in GitHub Desktop.
takeUntil :: (a -> Bool) -> [a] -> [a]
takeUntil _ [] = []
takeUntil p (x:xs)
| (not.p) x = x : takeUntil p xs
| otherwise = [x]
paso :: (Integer, Integer, Integer, Integer) -> (Integer, Integer, Integer, Integer)
paso (numimpares,secuencia,x,reg) = (numimpares',secuencia',x',reg') where
x' = quot x 2
corregido = if odd x then reg + 3^numimpares else reg
numimpares' = if odd corregido then numimpares + 1 else numimpares
secuencia' = corregido
reg' = (\n -> if even n then quot n 2 else quot (3*n + 1) 2) corregido
parcial :: Integer -> (Integer, [Bool])
parcial n = (n', secuencia) where
t = takeUntil filtro $ iterate paso (0,0,n,0)
filtro (_,_,x,_) = x == 0
(_,_,_,n') = last t
secuencia = tail $ map mapa t
mapa (_,a,_,_) = odd a
test n = takeUntil (==2) $ iterate iterador n where
iterador :: Integer -> Integer
iterador = (fst.parcial)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment