Skip to content
Go back

Divisors of a Number: Find and Count (Python Examples)

Published:  at  10:24 PM

Have you ever wondered how many pieces make up the soul of a number? 🧵 I’m not talking about its value, but about the number of numbers that divide it perfectly, revealing its internal structure. Let’s discover it together.

🔮 Problem Statement

Our challenge is to create a function that accurately determines the number of divisors a given integer has.

Parameters:

Returns:

Example:

>>> count_divisors(12375)
24
>>> count_divisors(3)
2
>>> count_divisors(10)
4
>>> count_divisors(1)
1

Additional Notes:

🧩 Step-by-Step Solution

We will start by defining the function that will receive the number as an argument. This function will be the heart of our solution.

def count_divisors(num):

We begin by creating a set called divisors with the values 1 and num. We use a set because it guarantees that we will not have duplicate values, something crucial to avoid counting the same divisor multiple times. In addition, searching in sets is much more efficient than in lists, which optimizes our algorithm, especially for large numbers. We assume that both 1 and the number itself are divisors.

    divisors = set([1, num])

Now we will iterate from 2 to num - 1. These are the possible intermediate divisors that we must verify. We do not need to go beyond the square root of num, but for simplicity in this example, we will iterate up to num - 1.

    for n in range(2, num):

Inside the loop, we check if num is divisible by n. If the remainder of the division is zero, it means that n is a divisor of num. The operator % is our ally to obtain the remainder of the division.

        if not num % n:

If we find that n is a divisor, we add it to the divisors set. The set will automatically avoid duplicates, which makes our job easier.

            divisors.add(n)

Finally, once the loop is completed, we return the number of elements in the divisors set. This number represents the total number of divisors we found for the original number.

    return len(divisors)

Complete Solution:

def count_divisors(num):
    divisors = set([1, num])
    for n in range(2, num):
        if not num % n:
            divisors.add(n)
    return len(divisors)

🧠 Key Concepts

The use of sets (set) is fundamental here. Unlike lists, sets guarantee the uniqueness of elements and offer a faster search (on average O(1) versus O(n) for lists). This is especially useful when working with large numbers, where efficiency is crucial.

Loops (for) are the tool that allows us to iterate through a range of numbers and verify if they are divisors of the given number. While we could further optimize this loop (limiting the iteration to the square root of the number), the key concept remains the systematic iteration.

Finally, the arithmetic modulo operator (%) is essential to determine if a number is divisible by another. This operator returns the remainder of a division, and if the remainder is zero, we know that the division is exact and, therefore, the divisor is valid.

Did you know…? The efficiency of an algorithm for finding divisors can be significantly improved by using the property that divisors come in pairs. If you find a divisor n of a number num, then num / n is also a divisor. Therefore, you only need to iterate up to the square root of num.

💫 Final Thoughts

Although this solution works, it is important to consider optimization for very large numbers. Limiting the loop to the square root of the given number would significantly improve efficiency. In addition, the use of prime factorization algorithms could be explored to obtain an even faster solution.

This function is a small but powerful building block in the construction of more complex algorithms. It teaches us the importance of choosing the right data structures (such as sets) and understanding the mathematical properties of numbers to optimize our code.

I hope this journey through the world of divisors has been interesting! If you want to continue exploring the mysteries of code and mathematics, I invite you to continue reading the articles on this blog. The next challenge awaits you! 🚀



Previous Post
Check if a Number is Odd with Python: Step-by-Step Guide
Next Post
Tree Growth: Cycles, Height and Calculation with Python