0% found this document useful (0 votes)
43 views

Week 11 - Recursion

The document discusses recursion and provides examples of recursive functions to solve problems like: 1) Factorial - A recursive function to calculate the factorial of a number. 2) Fibonacci - A recursive function to calculate the nth Fibonacci number. 3) Hailstones - A recursive function to calculate the hailstone sequence starting from a given number. Iterative solutions to these problems are also discussed. The advantages of recursion include elegance, while iterations are more efficient. Extensions on recursive solutions to problems like 24 game and Towers of Hanoi are presented.

Uploaded by

Vincent Zhang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Week 11 - Recursion

The document discusses recursion and provides examples of recursive functions to solve problems like: 1) Factorial - A recursive function to calculate the factorial of a number. 2) Fibonacci - A recursive function to calculate the nth Fibonacci number. 3) Hailstones - A recursive function to calculate the hailstone sequence starting from a given number. Iterative solutions to these problems are also discussed. The advantages of recursion include elegance, while iterations are more efficient. Extensions on recursive solutions to problems like 24 game and Towers of Hanoi are presented.

Uploaded by

Vincent Zhang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

INFO1110 / COMP9001 Week 11 Tutorial

Recursion

Topics: recursion.

Drills

Infinity
Examine the snippet of code below.

What does it do?


What happens if lines 2 and 3 are uncommented?
How could we modify spam to print out numbers, starting at n and ending at 0 ?
What about starting at 0 and ending at n ?

1 def spam(n):
2 # if n == 0:
3 # return n
4 return spam(n-1)
5
6 ham = spam(3) # what is the value of `ham`?

Factorial
The factorial of a non-negative integer is denoted by , and can be defined as:

Write a recursive function factorial(n) that returns the factorial of an integer n .

1 factorial(4) # 24
2 factorial(3) # 6
3 factorial(2) # 2
4 factorial(1) # 1
5 factorial(0) # 1
6 factorial(1000) # what happens here?
Problems

Binomial
Write a function binomial(n, k) that returns the binomial coefficient " choose ", defined as:

1 binomial(0, 0) # 1 (there is 1 way to choose 0 items from 0)


2 binomial(2, 1) # 2 (there are 2 ways to choose 1 item from 2)
3 binomial(4, 2) # 6 (there are 6 ways to choose 2 items from 4)
4 binomial(5, 3) # 10 (there are 10 ways to choose 3 items from 5)

Hint: Use your factorial function. Your binomial function should not be recursive!

Fibonacci
Write a recursive function fibonacci(n) that returns the n th Fibonacci number.

The Fibonacci sequence is: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, . . .

1 fibonacci(8) # 21
2 fibonacci(7) # 13
3 fibonacci(6) # 8
4 fibonacci(0) # 0

Hint: What are the base cases? (There are two.) What is the recurrence relation?

Hailstones redux
Recall the Hailstones problem (in the Week 3 tute):

Start with any positive integer n .

If n is even, divide it by 2 to get n/2 .


If n is odd, multiply it by 3 and add 1 to get 3n + 1 .

Repeat the above process indefinitely. The Collatz conjecture says that you will always eventually reach
the number 1 .

For example, starting with the number 6 , we get the numbers 6, 3, 10, 5, 16, 8, 4, 2, 1 , known
as the hailstone sequence or hailstone numbers.

Write a recursive function hailstones(n) that returns the hailstone sequence starting at the integer n .

1 hailstones(6) # [6, 3, 10, 5, 16, 8, 4, 2, 1]


2 hailstones(3) # [3, 10, 5, 16, 8, 4, 2, 1]
3 hailstones(10) # [10, 5, 16, 8, 4, 2, 1]
4 hailstones(1) # [1]
Hint 1: What is the base case? What is the recurrence relation?

Hint 2: [1, 2] + [3, 4] # evaluates to [1, 2, 3, 4]

Iteration
Write an iterative function to solve each of the Factorial, Fibonacci and Hailstones problems.

Note: You should probably attempt an iterative Fibonacci solution last—it is a bit tricky!

What are the advantages and disadvantages of a recursive implementation over an iterative one?

Extensions

Twenty Four
Have a look at the Twenty Four challenge on ed. Although it isn't obvious, this problem has a very nice
recursive solution.

Think about what the base case might be.

Recall that the base case is usually the smallest or simplest instance of a problem.
Think about what the recurrence relation might be.
How can the 24 problem be "reduced", until it reaches the base case?

If you've figured out the recursive solution, go ahead and implement it!

Hint 1: What numbers are mentioned in the challenge description? Could these numbers be made smaller?

Hint 2: Ironically, the recursive solution becomes more obvious if the challenge is "generalised".

Towers of Hanoi
The Towers of Hanoi is a mathematical puzzle, consisting of rods , and , and discs of different sizes.

Initially, all discs are placed on the leftmost rod , with the largest disc at the bottom and the smallest disc
at the top.

The objective is to move all the discs to another rod ( or ), while adhering to these rules:

only one disc can be moved at a time;


only a disc at the top of a rod can be moved;
a disc cannot be placed on top of a smaller disc.

Write a recursive function that prints all the steps to move discs to another rod.

You might also like