Lesson 3 – Representing algorithms
Tracing code
Task 1 . Russian multiplication
The Python code in Figure 1 is an implementation of the Russian multiplication
algorithm. This method calculates the product of two numbers as a sum by using
integer division and modulo (MOD). Use the table below to help you investigate the
algorithm in Python.
Explanation Python example
Modulo (MOD) Calculates the remainder of a division. For 7%3
example 7 MOD 3 will calculate as 1.
Integer division Calculates the whole number of times the 7 // 3
divisor (3) will go into the dividend (7). For
example 7 ÷ 3 will calculate as 2.
1 print("Numbers:")
2 a = int(input())
3 b = int(input())
4 sum = 0
5 while b > 0:
6 if b % 2 == 1:
7 sum = sum + a
8 a = 2*a
9 b = b // 2
10 print(sum)w
Figure 1
State the result of the following calculation in Python: 14 % 4
2
State the result of the following calculation in Python: 28 // 5
5
,