Creating Recursive Functions
For this activity, you will need to be using an Integrated Development Environment (IDE). The recommended IDE for this module is JetBrains IntelliJ IDEA for Python and JetBrains CLion for C++. You can follow instructions at the following page to set up your development environment:
Setting up your Development Environment
Task 1 - Factorial of a Number
For this task you will want to implement a recursive method that can provide the factorial of a number.
The factorial for a non-negative integer n
, n!
, is defined as:
The input to your program consists of several lines, each containing two non-negative integers, n
and m
,
both less than \(2^{31}\). For each input line, output a line stating whether n!
divides m
.
Example
Input
def factorial(int a, int b):
# Write your code to satisfy the equation above.
return NotImplemented
factorial(6, 9)
factorial(6, 27)
factorial(20, 8)
factorial(20, 27)
Output
6! divides by 9
6! does not divide by 27
20! divides by 8
20! divides by 27
Task 2 - Sorting Numbers in a Recursive Manner
For this task you will want to implement a recursive method that can sort a given list of numbers. You will be asked to implement the following sorting algorithms:
- Merge Sort
- Quick Sort
The list of numbers you are required to sort are:
listOfNumbers = [43, 53, 7, 61, 54, 58, 84, 28, 13, 100, 45, 76, 52, 50, 10, 5, 56, 8, 35, 6, 46, 72, 39, 25]
Struggling?
If you are struggling with this lab activity, you may want to get some additional support. You can speak to the module leader/team in the room of when the lab week is active, or you can visit the Additional Support page for more information on how you can get extra support.