Any pointers on how to solve efficiently the following function in Haskell, for large numbers (n > 108)
f(n) = max(n, f(n/2) + f(n/3) + f(n/4))
I've seen examples of memoization in Haskell to solve fibonacci numbers, which involved computing (lazily) all the fibonacci numbers up to the required n. But in this case, for a given n, we only need to compute very few intermediate results.
Thanks
Edward's answer is such a wonderful gem that I've duplicated it and provided implementations of
memoList
andmemoTree
combinators that memoize a function in open-recursive form.A couple years later, I looked at this and realized there's a simple way to memoize this in linear time using
zipWith
and a helper function:dilate
has the handy property thatdilate n xs !! i == xs !! div i n
.So, supposing we're given f(0), this simplifies the computation to
Looking a lot like our original problem description, and giving a linear solution (
sum $ take n fs
will take O(n)).