Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

How about the following Haskell program?

    rec ((x:xs),p) = (filter (/= p+x) xs,p+x)
    sequ = map snd $ iterate rec ([2..],1)
sequ is an infinite list of terms of the sequence A005228.



That just enumerates the entire sequence; I think the challenge is to do it faster than that.

By the way, the use of `filter` makes your implementation unnecessarily slow. (The posted link also contains Haskell code, which uses `delete` from Data.List instead of `filter`, which is only slightly better.)

I'd solve it like this, which generates both sequences in O(n) time, and the mutual recursion is cute:

    a005228 = 1 : zipWith (+) a005228 a030124

    a030124 = go 1 a005228 where
        go x ys
            | x < head ys = x     : go (x + 1) ys
            | otherwise   = x + 1 : go (x + 2) (tail ys)




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: