Skip to content

Question 21

What is the output of the following code?

1
2
3
4
5
6
7
coprimes = ()
for i in range(2, 6):
    for j in range(1, i):
        if i % j == 1:
            coprimes = coprimes + ((j,i),)

print(coprimes)
Solution
i j i % j == 1 coprimes (init. val.: ())
2 1 2 % 1 = 0 == 1 (False) ()
3 1 3 % 1 = 0 == 1 (False) ()
3 2 3 % 2 = 1 == 1 (True) ((2, 3))
4 1 4 % 1 = 0 == 1 (False) ((2, 3))
4 2 4 % 2 = 0 == 1 (False) ((2, 3))
4 3 4 % 3 = 1 == 1 (True) ((2, 3), (3, 4))
5 1 5 % 1 = 0 == 1 (False) ((2, 3), (3, 4))
5 2 5 % 2 = 1 == 1 (True) ((2, 3), (3, 4), (2, 5))
5 3 5 % 3 = 2 == 1 (False) ((2, 3), (3, 4), (2, 5))
5 4 5 % 4 = 1 == 1 (True) ((2, 3), (3, 4), (2, 5), (4, 5))
Note

It may be helpful if you manage to latch on the idea of looking for pairs of numbers whose j / i operation will produce remainder 1. Fun fact, that's what it means for 2 numbers to be coprime - you need not remember this for any test or exam for this module, this is purely for extra knowledge. It is useful in cryptography!!

Answer
((2, 3), (3, 4), (2, 5), (4, 5))