Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Computer Science 1001

Lecture 28

Lecture Outline

• Recursion

– CS1001 Lecture 28 –
Example: Euclid’s algorithm

• Recall Euclid’s algorithm to compute the greatest


common divisor:

def gcd(n1,n2):
r=1;
while r!=0:
q=n1//n2
r=n1%n2
n1=n2
n2=r
return n1

• We can write a recursive version of this function as


follows:

def gcd(n1,n2):
if n1 % n2 == 0:
return n2
else:
return gcd(n2,n1%n2)

• What happens if the gcd function is called with


n1 < n2?

– CS1001 Lecture 28 – 1
Example: Tower of Hanoi

• The Tower of Hanoi is a puzzle that involves moving


disks from one tower to another while observing
certain rules.

• We label the disks 1, 2, 3, . . . , n, where 1 is the top


disk.

• We label the towers A, B, and C, where the tower


starts at A and the goal is to move the tower to C.

• The following rules must be followed:


– we can only move one disk at a time and the disk
moved must be the top disk on a tower,
– no disk can be placed on a smaller disk at any
time.

– CS1001 Lecture 28 – 2
Example: Tower of Hanoi

• In the case of 3 disks we can easily illustrate the


solution.

• For 4 or more disks the problem is much more


difficult.

• This problem is inherently recursive and can be


solved with the following algorithm (where n is the
number of disks):

1. move the top n-1 disks from A to B


(with the help of C)
2. move disk n from A to C (the target)
3. move the n-1 disks from B to C
(with the help of A)

– CS1001 Lecture 28 – 3
Example: Tower of Hanoi

# move n disks from source to target with helper


def hanoi(n, source, helper, target):
if n==1:
print("Move disk",n,"from",source,"to",target)
else:
# move tower of size n-1 to helper:
hanoi(n-1, source, target, helper)
print("Move disk",n,"from",source,"to",target)
# move tower of size n-1 from helper to target
hanoi(n-1, helper, source, target)

def main():
n = eval(input("Enter number of disks: "))
print("The moves are:")
hanoi(n,’A’,’B’,’C’)

main()

– CS1001 Lecture 28 – 4
Recursive helper functions

• Suppose that we want to write an algorithm to


compute the square root of a number.

• Given a value x > 0 and a guess g for the square


root, a better guess is given by (g + x/g)/2.

• We will write a function called squareRoot(x)


to compute the square root of x to an accuracy of
1×10−14. This function will make use of a recursive
helper function called squareRootGuess(x,g).

– CS1001 Lecture 28 – 5
Example: Square root

# Compute a square root using recursion.

## Compute the square root of x.


# @param x the number to compute the square root of
# @param g the initial guess of the square root
# @return the square root of x, within 1e-14
def squareRootGuess(x, g) :
if abs(g * g - x) < 1e-14 :
return g
return squareRootGuess(x, (g + x/g) / 2)

## Compute the square root of x.


# @param x the number to compute the square root of
# @return the square root of x, within 1e-14
def squareRoot(x) :
return squareRootGuess(x, x)

def main() :
print("sqrt(2) is", squareRoot(2))

main()

Output:
sqrt(2) is 1.414213562373095

– CS1001 Lecture 28 – 6

You might also like