4 ms·
Coding exercise: write a function boolean isInSequence(n): that decides whether the given integer is part of that sequence or not. However, pre-storing th
by kleiba 1y ago
Coding exercise: write a function
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.
- rokob 1y agoreturn n >= 0
- vbezhenar 1y agoCompute the sequence until you get n or m > n?
- haskellshill 1y agoHow 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)
- asboans 1y agoI don’t know but I think I could probably implement IsInSequenceOrFirstDifferences(n)