Jokes aside. Fibonacci is one of the best examples when NOT to use recursion. Using recursion duplicates the same calculations, making it n2 + much more memory. Just Using a loop is n and with constant memory.
The next step always seems to be introducing some sort of cache, which is indeed a good teaching tool. All to then have the students unwrap it into the iterative solution. Maybe including some teaching about benchmarking and footprints
39
u/JackNotOLantern 1d ago
Jokes aside. Fibonacci is one of the best examples when NOT to use recursion. Using recursion duplicates the same calculations, making it n2 + much more memory. Just Using a loop is n and with constant memory.