Let’s discuss a common problem met during the interview process. The problem is defined as follows:

Given a number print out a list of all the divisors of that number.

Let’s spare a moment to understand what a divisor is.

💡

Divisorsare numbers that divide another number evenly, meaning the division result is a whole number with no remainder.

For instance, let’s find all divisors of number 12.

12 / 1 = 12

12 / 2 = 6

12 / 3 = 4

12 / 4 = 3

12 / 5 = 2

*with a remainder of*212 / 6 = 2

12 / 7 = 1

*with a remainder of*512 / 8 = 1

*with a remainder of*412 / 9 = 1

*with a remainder of*312 / 10 = 1

*with a remainder of*212 / 11 = 1

*with a remainder of*112 / 12 = 1

Numbers 1, 2, 3, and 4 are divisors of 12 as there is no reminder. Number 5 is not a divisor of 12 as there is a reminder of 2. Number 6 is a divisor. Numbers 7, 8, 9, 10, and 11 are not divisors of 12. Lastly, 12 is a divisor of 12. Summarizing, numbers 1, 2, 3, 4, 6 and 12 are divisors of 12.

# Solution

Let’s start with defining a function that takes a number and returns a list of divisors.

```
def divisors(n: int) -> list[int]:
l = []
for i in range(1, n + 1):
if n % i == 0:
l.append(i)
return l
if __name__ == '__main__':
number: int = int(input(f'Provide a positive number\n'))
divisors_list: list[int] = divisors(number)
print(f'Number {number} has the following divisors: {divisors_list}')
```

To solve the current problem we use the *range()* function to loop from 1 to the provided number plus one. During each iteration, we compute modulo from the provided number and the current one coming from the range. If the modulo operation gives zero, then the current number is a divisor of the provided one. In that case, we add it to the list. Let's run it and provide number 12.

```
Provide a positive number
12
Number 12 has the following divisors: [1, 2, 3, 4, 6, 12]
```

The list with numbers includes those that we have previously calculated manually.

# Summary

Divisors are numbers that divide another number evenly. To check whether a number is a divisor we can use the modulo operator. To obtain a list of divisors we can use the *range()* function for that purpose.