Math’s relation to programming

Hanin Emad
3 min readOct 6, 2023

--

Math’s relation to programming

The relationship between Mathematics and Programming is fundamental and closely intertwined 🔀 , not because I’m a math lover or my teacher on day told me that everything is about math, but because that Mathematics provides the foundation for designing algorithms, which are step-by-step instructions for solving problems that helps programmers analyze and break down complex problems into smaller, manageable component.

The Tower of Hanoi example that a lot of programmers heard about somehow, is a popular puzzle in mathematics and programming which is a good example of what we want to say and talk about today.

The puzzle was presented to a young priest who was told to move all the disks from one post to another without violating a set of given rules.

The objective is to move (n) number of disks from one tower to another following a set of rules.

Rules are as follows: 🛑

  • Only one disk can be moved at a time
  • Only the upper disk of any of the towers can be moved
  • Larger disks cannot be placed over smaller disks

If the priest followed the rules and moved one disk per second, the puzzle would be solved in 2⁶⁴ — 1 second. That’s about 585 billion years. 😴 By then the temple would no longer exist. Inspired by this legend, a French mathematician Edouard Lucas invented the Tower of Hanoi puzzle more than a hundred years ago.

In the scenario of solving the puzzle, the total moves will be 2^n — 1 where n is the number of disks that need to be moved. 💡 💡

The code for this in Python uses the principles of Recursion is in the third picture.

>> explain the code <<

In the block of code, the first section is the base condition that you apply when using disk 1. Once executed, it returns to the rest of the execution flow out of the if condition.

The remaining disks are moved by passing the values from source to helper with destination as helper. The remaining disk is moved from source to destination. The remaining n-1 disks on the helper are moved from helper to destination with source as the helper.

In the last section, the driver code takes the input for the number of disks I want to move. In accordance, I pass the names of the towers and give the function call.

So, if we have 3 towers it will be 2³ — 1 = 7 steps to complete the transfer. 📣

def hanoi(disks, source, helper, destination):
# Base Condition
if (disks == 1):
print('Disk {} moves from tower {} to tower {}.'.format(disks, source, destination))
return

# Recursive calls in which function calls itself
hanoi(disks - 1, source, destination, helper)
print('Disk {} moves from tower {} to tower {}.'.format(disks, source, destination))
hanoi(disks - 1, helper, source, destination)

# Driver code
disks = int(input('Number of disks to be displaced: '))

# Actual function call
hanoi(disks, 'source', 'helper', 'destination')

--

--

Hanin Emad

A Database engineer with a bachelor degree in Telecommunication engineering. Alumna of the International exchange community funded by the USA government.