Tutorial 9 Worksheet
COMP1117B Computer Programming 2023-2024
Tutorial objectives:
• To have a better understanding of Python function
• To understand the way of using recursion to solve a problem
• Deadline: 23:59 Apr 23, 2024
Exercise 9.1 Even product calculator
Implement a recursive function to calculate the product of all the positive even numbers not larger
than n (n>=2). For example, if n = 7, the return value of this function will be 48. That’s because:
1. All the positive even number no larger than n = 7 is 6, 4, and 2
2. Their production is 6 * 4 * 2 = 48
Below it’s the given program
def product_of_even(m):
# Your implementation
print( product_ of_ even( int( input( ) ) ) )
Below it’s the test cases
Tase Case 1
Input:
3
Output:
2
Tase Case 2
Input:
5
Output:
8
Tase Case 3
Input:
10
Output:
3840
Tase Case 4
Input:
20
Output:
3715891200
Tase Case 5
Input:
15
Output:
645120
1 IN 4
Exercise 9.2 Multiply two number
Implement a recursive function multiply(a, b) to calculate the value of a*b without using the *
operator.
• a, b >= 0 and min(a, b) <= 1000
• Must use recursive function to solve this problem
• Don’t use the * operator in your implementation
• Try to avoid some unnecessary computation in your implementation by the if-else statement.
Otherwise, it may cause runtime errors for some of the cases
• Hint: a * b = a + a * (b - 1) or b + b * (a - 1); Try to use this to decrease the input size of your
function
Below it is the given program
def multiply(a, b):
# Your implementation
a = int(input())
b = int(input())
print(multiply(a, b))
Below are the test cases
Test Case 1
Input:
0
0
Output:
0
Test Case 2
Input:
50
100
Output:
5000
Test Case 3
Input:
1
1000
Output:
1000
Test Case 4
Input:
1000
1
Output:
1000
2 IN 4
Exercise 9.3 Special sequence
There is a number sequence: 2, 1, 3, 2, 3, 6, 6, 18, 36, 108, 648, 3888, 69984 …
Try to implement the func(n) to calculate the nth value of this number sequence.
• n >= 0,
• Must use the recursive function to solve this problem
• Hint: In this number sequence, the first 3 numbers (2, 1, 3) are fixed; The fourth number is
calculated as the product of the first two numbers. The rest of the numbers in the following
sequence will follow the same pattern.
Below it is the given program
def func(n):
# Your implementation
n = int(input())
print(func(n))
Below are the test cases
Test case 1
Input:
1
Output:
1
Test case 2
Input:
20
Output:
5798976440091547641555385157483998407888470016
Test case 3
Input:
15
Output:
176319369216
3 IN 4
Exercise 9.4 Digit calculator
Try to implement func(n, digit) to calculate the times that the digit (0 <= digit <=9) shown in the
interval [1, n].
For instance, if n = 12, digit = 2, the output will be 2.
Because in [1, 12], all the numbers are 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12. There are two numbers
containing the digit 2,which are 2, and 12. So the return value in this case is 2.
Another example is that if n = 11, digit = 1, the output will be 4.
Because in [1, 11], all the numbers are 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11. There are 3 numbers
containing the digit 1,which are 1, 10, and 11. Furthermore, there are two ‘1’ in the number 11.
Therefore, the return value in this case is 1 + 1 + 2 = 4.
Below is the given program
def func(n, digit):
# Your implementation
n = int(input())
digit = int(input())
print(func(n, digit))
Below are the test cases
Test case 1
Input:
11
1
Output:
4
Test case 2
Input:
22
2
Output:
6
Test case 3
Input:
20
1
Output:
12
4 IN 4