Brain teasers: Divisors

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

Brain teasers: Divisors

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.

💡
Divisors are 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 2
  • 12 / 6 = 2
  • 12 / 7 = 1 with a remainder of 5
  • 12 / 8 = 1 with a remainder of 4
  • 12 / 9 = 1 with a remainder of 3
  • 12 / 10 = 1 with a remainder of 2
  • 12 / 11 = 1 with a remainder of 1
  • 12 / 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.

Let’s crack the problem now. If you need to refresh your knowledge, follow the link below. 👇👇👇

Exploring Python
Python is frequently utilized in creating websites and software, as well as for automating tasks, analyzing data, and visualizing information.

Solution

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