0% found this document useful (0 votes)
6 views4 pages

Tutorial 5 Worksheet: COMP1117A Computer Programming I 2018-2019

The document provides a tutorial worksheet for COMP1117A Computer Programming I, detailing objectives and exercises focused on recursive functions in Python. It includes midterm solutions for two programming questions, exercises on combinations, factorials, and the Ackermann function, along with expected outputs for given inputs. The deadline for submission of the exercises is November 8, 2018.

Uploaded by

mikewaics
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Tutorial 5 Worksheet: COMP1117A Computer Programming I 2018-2019

The document provides a tutorial worksheet for COMP1117A Computer Programming I, detailing objectives and exercises focused on recursive functions in Python. It includes midterm solutions for two programming questions, exercises on combinations, factorials, and the Ackermann function, along with expected outputs for given inputs. The deadline for submission of the exercises is November 8, 2018.

Uploaded by

mikewaics
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Tutorial 5 Worksheet

COMP1117A Computer Programming I 2018-2019

Tutorial objectives:
- Midterm solutions of Question A and B
- To practice the use of recursive functions in Python.
Tutorial 5 Exercises Deadline: Nov 8, 2018 14:20

1. Midterm solutions

Demo: Question A
Write a complete Python program that reads an integer, then multiply it by 2, and print the
sum of the digits in the result. For example, if the input is 123, then your program should
output 12 because 123 × 2 = 246 and 2 + 4 + 6 = 12 .
Note: You may assume that the input contains only the digits ‘0’, ‘1’, ‘2’, …, ‘9’.

- Firstly, read an integer from the user.

n = int(input())

- Next, multiply the number by 2.

n = n * 2

- Then we should find out the digits one by one in the number.

sum = n % 10
while n >= 10:
n = n // 10
sum = sum + n % 10

- Finally, print the sum.

print(sum)
Demo: Question B
A positive integer is interesting if it is the sum of two prime numbers. Write a complete Python
program that inputs a positive integer m, and print the character ‘Y’ if m is interesting, and
print the character ‘N’ otherwise. Again, you may assume the input contains only digits.
Note: 1 is not a prime number, and any integer greater than 1 is a prime number if it is only
divisible by 1 and itself.

- Firstly, read a positive integer m from the user.

m = int(input())

- Next, write a function to check a given number to be prime number or not.

def prime(n):
if n == 1:
return False

for i in range(2, n):


if n % i == 0:
return False
return True

- Use a boolean variable to store the answer.

flag = False

- Then check the number one by one to see any pairs of numbers could give the sum as the
input number and both of them are prime.

for j in range(1, m):


if prime(j):
if prime(m - j):
flag = True

- Finally, print the result.

if flag:
print('Y')
else:
print('N')
Exercise 5.1 Combination
In mathematics, “​combinations refer to the combination of ​n​ things taken ​k​ at a time without
repetition”. [1] There is a recursive formula related to combinations:
nCr = n − 1Cr − 1 + n − 1Cr
And nC0 = nCn = 1 , nCk = 0 if k > n .
Write a Python program to calculate the value of nCr of the given n and r by using this
recursive function.

Remarks: For more information about Combination, you may visit [1]
[1]: https://en.wikipedia.org/wiki/Combination

Input Expected output

6 20
3

8 8
1

Exercise 5.2 Factorial

Write a program to calculate the factorial of a given number n by using the recursive function.
n! = n × (n − 1)!
And 0! = 1 .

Remarks: For more information about Factorial, you may visit this website
https://en.wikipedia.org/wiki/Factorial

Input Expected output

1 1

5 120
Exercise 5.3 Ackermann function

Implement Ackermann function by recursive function calls. Ackermann function is defined as


follows:
A(m, n) = n + 1 if m = 0
A(m, n) = A(m − 1, 1) if m > 0 and n = 0
A(m, n) = A(m − 1, A(m, n − 1)) if m > 0 and n > 0.

Remarks: For more information about Ackermann function, you may visit this website
https://en.wikipedia.org/wiki/Ackermann_function

Input Expected output

1 4
2

4 13
0

You might also like