
Algorithms affect us in our everyday life.
Diy hanoi towers software#
In fact, I think it’s not only important for software development or programming, but for everyone. Photo by bruce mars on Unsplash What is an algorithm?Īn algorithm is one of the most important concepts for a software developer. What is that? Any idea? No problem, let’s see. Wait, we have a new word here: “ Algorithm”. Now, let’s try to build the algorithm to solve the problem. You can see the animated image above for a better understanding. Let’s go through each of the steps:īoom! We have solved our problem. We can use B as a helper to finish this job.

How do we do this?īefore we can get there, let’s imagine there is an intermediate point B. Our job is to move this stack from source A to destination C.

You can move any disk to the second post as a resting area.

You can only move one disk at a time from one post to another, which means at any given time, at most one disk can be off a post.The goal is to move all disks from the first post to the third post. That puzzle is shown in the image on this page. If you keep going down the chain, you will get to the base case of summing whole numbers between 1 and 1, which we know is 1.Ī fun use of recursion is to write a program that can give you the solution to the Tower of Hanoi puzzle. For the next step of the example above, to get the sum of whole numbers between 1 and 99, you simply sum the whole numbers between 1 and 98, then add 99. Silly, right? This is actually an example of a powerful divide-and-conquer technique in computer programming known as "recursion." Recursion treats a computation task as a set of smaller, similar computations, with the next smaller computation being calculated, and then the next, and the next, until you get to a "base case" with a known answer. Q: What's the easiest way to get the sum of all whole numbers between 1 and 100?Ī: First get the sum of all whole numbers between 1 and 99, then add 100.
