Moods
✅ Fibonacci sequence
while the first code snippet is right(and even sorta optimal)...that's a pretty bad way to explain the point of memoization
Memoization exists for situations where there is redundant calculations being done.
for example here,
If i used
nth_number
as something like 5, it'd start off as a call to Fib(5)
, then
Fib(5)
calls (case a)Fib(4)
and Fib(3)
for case a:
Fib(4)
calls Fib(3)
and Fib(2)
Fib(3)
calls Fib(2)
and Fib(1)
, then Fib(2)
calls Fib(1)
and Fib(0)
--ends here
Fib(2)
(from the Fib(4)
call) is done again (an example of a redundant call as Fib(2)
was already done)
for something like Fibonacci it wouldn't matter too much to have redundant calls, but for situations similar in nature to it with stuff like expensive calculations in each function call, it pays to have redundant calls.
That's where memoization comes in, it caches(stores) each call once completed so that if this call were to happen again it would not need to do the expensive calculations again, it just returns the cached call.
A way to properly implement memoization in Fibonacci would be
33 replies
Parallel list
there is also https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.parallel.foreachasync?view=net-8.0
4 replies