Lesson 24
Divisibility - % in conditions
After this lesson: You'll check whether a number divides by another with no remainder, and recognise even numbers.
Video
The video for this lesson is coming soon.
Explanation
Remember % from lesson 5? It gives the remainder of a division: 14 % 3 is 2, because 14 = 4 ยท 3 + 2. Today we'll use it for the most important question: does something divide with no remainder? It's the key to lots of contest tasks! ๐
Teams in PE class ๐ฌ
Click Next - you'll see what dividing with no remainder means.
Teams of three
There are 12 children in PE, and the teacher splits them into teams of 3 for 3x3 basketball. We line the children up in rows of 3...
Will the teams be equal?
14 children in rows of 3 - watch what is left at the end.
Click the arrow - we'll lay the number out in rows and see whether anything is left.
1children = 142if children % 3 == 0:3print("Equal teams!")4else:5print("Someone is left without a team")
โ15 children
One more child - and everything fits.
children = 15print("Equal teams!")print("Someone is left without a team")Click the arrow - I'll walk the program's path, line by line.
1children = 152if children % 3 == 0:3print("Equal teams!")4else:5print("Someone is left without a team")
โEven or odd?
The most common question about divisibility is: is the number even? Even = divides by 2 with no remainder:
| Condition | Means |
|---|---|
n % 2 == 0 | n is even (2, 4, 10, 0, -6...) |
n % 2 != 0 | n is odd (1, 7, 15, -3...) |
The pairs of socks ๐งฆ from lesson 5: an even number of socks - all of them have a pair. Odd - one is left alone.
Socks in pairs ๐งฆ
Type a number of socks ๐งฆ (e.g. 7, at most 60) and press Enter โ - you'll see whether every one has a pair.
Click the arrow - we'll lay the number out in rows and see whether anything is left.
1socks_str = input()2socks = int(socks_str)3if socks % 2 == 0:4print("Even - every sock has a pair")5else:6print("Odd - one is left alone")
โBy any number you like
We check divisibility by any number the same way:
n % 5 == 0- ends in 0 or 5 (5, 10, 35, 100...),n % 10 == 0- ends in 0 (a round number),n % 7 == 0- it's in the 7 times table (7, 14, 21...),a % b == 0- b is a divisor of a (e.g. 3 is a divisor of 12).
Egg boxes ๐ฅ
A box holds 6 eggs. Type a number of eggs ๐ฅ (at most 60) and press Enter โ - will all the boxes be full?
Click the arrow - we'll lay the number out in rows and see whether anything is left.
1eggs_str = input()2eggs = int(eggs_str)3if eggs % 6 == 0:4print("All the boxes are full!")5else:6print("One box isn't full")
โThe Pif-Paf game ๐ฌ
Click Next - a game where the order of the conditions is a trap.
The rules
The children count in turn. Whoever gets a number divisible by 3 says Pif. Divisible by 5 - Paf. And divisible by both 3 and 5 (15, 30...) - PifPaf! Otherwise you just say the number.
Play Pif-Paf ๐ฒ
Type a number ๐ข (e.g. 9, 10, 30 or 7) and press Enter โ - what do you have to say?
n_str = input()n = int(n_str)print("PifPaf")print("Pif")print("Paf")print(n)Click the arrow - I'll walk the program's path, line by line.
1n_str = input()2n = int(n_str)3if n % 15 == 0:4print("PifPaf")5elif n % 3 == 0:6print("Pif")7elif n % 5 == 0:8print("Paf")9else:10print(n)
โWhich condition checks whether n is odd?
What will this program print?
n = 20if n % 6 == 0:print("A")else:print(n % 6)
Which number should be checked first in Pif-Paf?
Remember
n % k == 0- n divides by k with no remainder.n % 2 == 0- even,n % 2 != 0- odd.a % b == 0means b is a divisor of a.- When conditions are inside each other (15 โ 3 and 5), check the most demanding one first.
On to the tasks! Each one is a question: does something divide with no remainder?
Tasks from the video
We solve them together in the video - pause it and try first on your own.
Practice tasks
Try to solve them without the video. Stuck? Every task has hints and an editorial.
Sign in to save your progress.