by kleiba · 1y ago · 0 comments
boolean isInSequence(n):
that decides whether the given integer is part of that sequence or not. However, pre-storing the sequence and only performing a lookup is not allowed. 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.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)