Solutions: You can find the file with solutions for all questions here.

Quiz submissions were graded automatically for correctness. Implementations did not need to be efficient, as long as they were correct.

In addition to the doctests provided to students, we also used extra doctests to check for corner cases. These extra test cases are highlighted below.

### Question 1: Diff

Implement a function `diff` that takes three integers `x`, `y`, and `z`. It returns whether subtracting one of these numbers from another gives the third.

``````def diff(x, y, z):
"""Return whether one argument is the difference between the other two.

x, y, and z are all integers.

>>> diff(5, 3, 2) # 5 - 3 is 2
True
>>> diff(2, 3, 5) # 5 - 3 is 2
True
>>> diff(2, 5, 3) # 5 - 3 is 2
True
>>> diff(-2, 3, 5) # 3 - 5 is -2
True
>>> diff(-5, -3, -2) # -5 - -2 is -3
True
>>> diff(-2, 3, -5) # -2 - 3 is -5
True
>>> diff(2, 3, -5)
False
>>> diff(10, 6, 4)
True
>>> diff(10, 6, 3)
False

>>> diff(9, 3, 6)
True
>>> diff(6, 3, -9)
False

"""
return x-y==z or y-z==x or z-x==y``````

Use OK to test your code:

``python3 ok -q diff``

### Question 2: Abundant

Implement a function `abundant` that takes a positive integer `n`. It prints all ways of multiplying two positive integers to make `n`. It returns whether `n` is an abundant number, meaning that the sum of its proper divisors is greater than `n`. A proper divisor of `n` is an integer smaller than `n` that evenly divides `n`.

Hint: To print `1 * 2`, use the expression `print(1, '*', 2)`

``````def abundant(n):
"""Print all ways of forming positive integer n by multiplying two positive
integers together, ordered by the first term. Then, return whether the sum
of the proper divisors of n is greater than n.

A proper divisor of n evenly divides n but is less than n.

>>> abundant(12) # 1 + 2 + 3 + 4 + 6 is 16, which is larger than 12
1 * 12
2 * 6
3 * 4
True
>>> abundant(14) # 1 + 2 + 7 is 10, which is not larger than 14
1 * 14
2 * 7
False
>>> abundant(16)
1 * 16
2 * 8
4 * 4
False
>>> abundant(20)
1 * 20
2 * 10
4 * 5
True
>>> abundant(22)
1 * 22
2 * 11
False
>>> r = abundant(24)
1 * 24
2 * 12
3 * 8
4 * 6
>>> r
True

>>> r = abundant(25)
1 * 25
5 * 5
>>> r
False
>>> r = abundant(156)
1 * 156
2 * 78
3 * 52
4 * 39
6 * 26
12 * 13
>>> r
True

"""
d, total = 1, 0
while d*d <= n:
if n % d == 0:
print(d, '*', n//d)
total = total + d
if d > 1 and d*d < n:
total = total + n//d
d = d + 1

Use OK to test your code:

``python3 ok -q abundant``

### Question 3: Amicable

Implement a function `amicable` that takes a positive integer `n`. It returns the smallest amicable number greater than `n`.

Two different numbers are both amicable if the sum of the proper divisors of each is equal to the other. Any number that's part of such a pair is an amicable number.

Hint: You may want to create a separate function to sum proper divisors.

``````def amicable(n):
"""Return the smallest amicable number greater than positive integer n.

Every amicable number x has a buddy y different from x, such that
the sum of the proper divisors of x equals y, and
the sum of the proper divisors of y equals x.

For example, 220 and 284 are both amicable because
1 + 2 + 4 + 5 + 10 + 11 + 20 + 22 + 44 + 55 + 110 is 284, and
1 + 2 + 4 + 71 + 142 is 220

>>> amicable(5)
220
>>> amicable(220)
284
>>> amicable(284)
1184
>>> r = amicable(5000)
>>> r
5020

>>> amicable(100483)
100485

"""
n = n + 1
while True:
m = sum_divisors(n)
if m != n and sum_divisors(m) == n:
return n
n = n + 1

def sum_divisors(n):
d, total = 1, 0
while d < n:
if n % d == 0:
total = total + d
d = d + 1
``python3 ok -q amicable``