Multiplication
Multiplication is a fast way to add the same number many times: means six added four times, or four added six times. It also counts grids and areas, scales amounts up, and is the operation behind most of the arithmetic a computer does in graphics and machine learning.
Words and symbols
In , the numbers being multiplied (7 and 8) are factors, and the result (56) is the product. Besides , you will see a centred dot (), brackets (), and in code an asterisk: 7 * 8.
Three pictures of the same idea:
- Repeated addition: .
- Groups: 3 bags with 5 apples each hold 15 apples.
- Arrays and area: a grid with 3 rows and 5 columns has 15 squares; a 3 cm by 5 cm rectangle has an area of .
● ● ● ● ●
● ● ● ● ● 3 rows × 5 columns = 15
● ● ● ● ● turn it sideways: 5 rows × 3 columns = 15The grid shows that order does not matter: .
Special factors
| Rule | Example | Why |
|---|---|---|
| one group of 47 | ||
| zero groups hold nothing | ||
| adds a zero | every digit moves up one place | |
| adds two zeros | every digit moves up two places | |
| doubling twice |
" adds a zero" is place value: 4 tens and 7 ones become 4 hundreds and 7 tens.
Splitting a multiplication
The most useful trick in multiplication is to split one factor into easy parts, multiply each part, and add:
This is the distributive property (more in Properties of Operations). Long multiplication on paper is just this trick done systematically, one digit of the second factor at a time:
3 4 7
× 2 6
-------
2 0 8 2 347 × 6
+ 6 9 4 0 347 × 20
---------
9 0 2 2The second row ends in 0 because the 2 in 26 is really 20. The same computation in Python, one partial product per digit:
a, b = 347, 26
partials = []
place = 1
for digit in reversed(str(b)):
p = a * int(digit) * place
print(f"{a} x {int(digit) * place:>2} = {p}")
partials.append(p)
place *= 10
print("sum of partial products:", sum(partials))
print("check:", a * b)347 x 6 = 2082
347 x 20 = 6940
sum of partial products: 9022
check: 9022
The times table
Knowing products up to by heart makes everything else in this course faster. Python can print the table, and a nested loop shows how the rows are built:
size = 9
print(" " + "".join(f"{c:4d}" for c in range(1, size + 1)))
for r in range(1, size + 1):
print(f"{r:2d} " + "".join(f"{r * c:4d}" for c in range(1, size + 1))) 1 2 3 4 5 6 7 8 9
1 1 2 3 4 5 6 7 8 9
2 2 4 6 8 10 12 14 16 18
3 3 6 9 12 15 18 21 24 27
4 4 8 12 16 20 24 28 32 36
5 5 10 15 20 25 30 35 40 45
6 6 12 18 24 30 36 42 48 54
7 7 14 21 28 35 42 49 56 63
8 8 16 24 32 40 48 56 64 72
9 9 18 27 36 45 54 63 72 81
The table is symmetric across its diagonal (because ), so there are only 45 facts to learn, not 81. The diagonal (1, 4, 9, 16, …) holds the square numbers, which return in Exponents.
Tip: For the 9s, the digits of each product add up to 9 (18, 27, 36, …), and the tens digit is one less than the number you multiply by: .
Multiplying many numbers
Python's math.prod multiplies a whole list, just as sum adds one. Multiplying several choices together counts combinations: 3 shirts 4 trousers 2 pairs of shoes give 24 outfits.
import math
print("outfits:", math.prod([3, 4, 2]))
print("pixels in a 1920 x 1080 screen:", 1920 * 1080)
print("bytes in that screen at 4 bytes per pixel:", 1920 * 1080 * 4)outfits: 24
pixels in a 1920 x 1080 screen: 2073600
bytes in that screen at 4 bytes per pixel: 8294400
In software
- Sizes and memory: an image of pixels, each with 4 colour bytes, needs bytes.
- Grids: a 2D grid stored in one list puts cell (row, col) at index
row * width + col. Games and spreadsheets use this constantly. - Nested loops run times, which is how you estimate a program's running time (see Growth Rates and Big-O).
- Neural networks spend almost all their time multiplying numbers and adding the results: a matrix multiplication is millions of small "multiply and add" steps.
Exercises
Exercise 1: Split it
Compute in your head by splitting 57.
Answer
456
Solution
Exercise 2: Long multiplication
Multiply by hand, showing the partial products.
Answer
11 186
Solution
print(238 * 7, 238 * 40, 238 * 7 + 238 * 40, 238 * 47)1666 9520 11186 11186
Exercise 3: Grid index
A game map is 12 cells wide and stored in one list, row by row. At which index is the cell in row 3, column 5 (both counted from 0)? Which row and column is index 50?
Answer
Index 41; index 50 is row 4, column 2.
Solution
. Going back uses division: and (check: ).
width = 12
print(3 * width + 5)
print(50 // width, 50 % width)41
4 2
Exercise 4: Count the outfits
A sandwich shop offers 4 breads, 6 fillings and 3 sauces, and you pick one of each. How many different sandwiches are possible? Check by listing them all with nested loops and counting.
Answer
72
Solution
. Each bread can go with each of 6 fillings (24 pairs), and each pair with each of 3 sauces.
count = 0
for bread in range(4):
for filling in range(6):
for sauce in range(3):
count += 1
print(count, 4 * 6 * 3)72 72
Test yourself
What is ?
- 248
- 288
- 268
- 308
Why does the second row of a long multiplication like end in a zero?
- It is a placeholder because that digit of 26 means 20
- It is a mistake
- Every product ends in zero
- To make the numbers line up nicely, with no meaning
Any number multiplied by 0 gives…
- the number itself
- 1
- 0
- it depends on the number