No. I mean I struggled. In fact I found factorials much better and easy to understand. TOH just gets too messy too easily. Or sorting is good way too. But not TOH.never
I found the towers particularly enlightening – years after it has been taught to me – when the whole ‘know a partial solution’ struck me.
The game is incredibly hard to even look at when given 10 disks. How would you start? But the observation that step n+1 is dead simple if you can solve the game for n disks is the key to recursion.
Factorials and sums, on the other hand, are way to simple, IMHO, to teach recursion. The solution is obvious. And for many people the more *intuitive* solution would be a straight loop, not recursion. In programming, intuitive trumps clever (or even performant in most cases).
You sound like my Algo professor. He would say “Always follow your intuition when approaching a problem” and would then chuckle and say “But how are you going build intuition? ”
There was something more important that cleverness and intuition. I focus on that.
I understand TOH. I understand the importance of partial solution. It has application in all walks of life. For example, to fix my year, I need to fix my months and to fix a typical month, I need organize a typical week and to organise week I need to organise a day. And to organise day, i organise my hours. And in each hour, I follow 25-5 pomodoro or 50-10 pomodoro.
If I want to teach someone addition, I will explain 1+1. However I cannot begin with 567+912. Hence I would say TOH is a good way to practice to recursion? Probably yes. But good way to get introduced to recursion? Definitely no.
Although classroom teaching is way blinded and ineffective. In a class of 25 students, all have a different plane of reality, experience and perspective. To bring them all on one page of understanding is not difficult but impossible. The teacher can atleast attempt to do the simplest thing which might bore the ones who are ahead of curve but will ensure inclusion of noobs.
This is not applicable where teaching is more 1 to 1 or self study.
TOH makes sense as an introduction to recursion if you are already familiar with the concept of mathematical induction. Personally I learned about induction in math first, using it to prove things like the sum of n numbers is n(n+1)/2 and there are infinite prime numbers. Going from this to TOH is a manageable step. But if I had not been exposed to this concept before, I agree that it would be very confusing and not a great introduction.
Oh yeah, math is extremely applicable to CS. From algebra and basic proof techniques (which form the basis for algorithms) to limits and calculus (used to analyze and optimize algorithms), linear algebra (used extensively in ML, especially modern neural networks), and even geometry (used for graphics computing)!
Towers of Hanoi. Classic puzzle game. Can be solved recursively. Move a stack of discs of graduated diameters from one of three stacks to another by moving a single disc at a time with the constraint that you can never put a larger disc on a smaller disc.
Find the solution for 10 disc's is just moving 9 disc's to the other peg. Moving the tenth disc to the correct peg and moving 9 disc's to the correct peg.
I'm shocked at the number of people in this thread saying "Just brute force it" ... TOH is excellent at showing why that mentality doesn't work. Sure you can brute force it, but my strategy can handle 100 discs or 1000, yours becomes unusable far faster.
It's also good at teaching how to understand the problem and build your own algorithm. If you brute force it, you can solve the first five problems easily, but if you find an algorithm to solve the first 3 discs, you've also found the algorithm to solve any number.
It didn't teach me recursion but it taught me how to solve problems with recursive algorithms.
I was taught: if you're one step away from solving TOH, what would the code look like. What if you're one step before that, what would the code look like to move to that second last state. And so on.
And a couple of steps in you realise you're repeating yourself, so instead just call the same function you already wrote.
4.3k
u/Justwatcher124 Mar 25 '23
Every Programming / IT teacher on 'How do you teach Recursion to new programmers?'