3 ms·
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
by haskellshill 1y ago
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.
- sltkr 1y agoThat 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)