How to Find Factors of a Number in Python?

In this tutorial, I will explain how to find factors of a number in Python. As a Python developer based in the USA, I recently needed to create a program that could efficiently identify the factors of any positive integer. After researching various methods, I discovered an effective approach and I will share it with you with examples.

Factors

Before we get into the code, let’s define what factors are. A factor is a number that divides another number evenly, leaving no remainder. For instance, the factors of 12 are 1, 2, 3, 4, 6, and 12 because each of these numbers divides 12 without leaving a remainder.

Read How to Format Numbers as Currency in Python?

Design the Algorithm

To find the factors of a number in Python, we can follow these steps:

  1. Create a function that takes a positive integer as input.
  2. Use a for loop to iterate from 1 to the input number.
  3. Check if each number in the loop divides the input number evenly using the modulo operator (%).
  4. If a number divides the input evenly, add it to a list of factors.
  5. Return to the list of factors.

Let’s implement this algorithm in Python.

Check out How to Get the Decimal Part of a Number in Python?

Implement the Solution

def find_factors(num):
    factors = []
    for i in range(1, num + 1):
        if num % i == 0:
            factors.append(i)
    return factors

Explanation of the code:

  • We define a function find_factors that takes a number num as input.
  • We create an empty list called factors to store the factors we find.
  • We use a for loop to iterate from 1 to num (inclusive).
  • Inside the loop, we check if num is divisible by i using the modulo operator (%). If the remainder is 0, it means i is a factor of num.
  • If i is a factor, we append it to the factors list using the append() method.
  • Finally, we return the factors list containing all the factors of num.

Read How to Check if a String is a Valid Date in Python?

Try Out the Function

Now that we have our find_factors function, let’s test it with some examples.

print(find_factors(24))

print(find_factors(36))

john_age = 30
print(find_factors(john_age))  

emma_favorite_number = 42
print(find_factors(emma_favorite_number))  

Output:

[1, 2, 3, 4, 6, 8, 12, 24]
[1, 2, 3, 4, 6, 9, 12, 18, 36]
[1, 2, 3, 5, 6, 10, 15, 30]   
[1, 2, 3, 6, 7, 14, 21, 42]

I have executed the above example code and added the screenshot below.

Factors of a Number in Python

In the examples above, we use the find_factors function to find the factors of various numbers, including some USA-specific names like John’s age and Emma’s favorite number.

Read How to Convert Month Name to Number in Python?

Optimize the Solution

While the current solution works well, we can optimize it further to improve efficiency. One optimization is to only iterate up to the square root of the input number. This is because if a number n has a factor f greater than its square root, then n must also have a corresponding factor n/f that is less than its square root.

Here’s the optimized version of the find_factors function:

import math

def find_factors_optimized(num):
    factors = []
    for i in range(1, int(math.sqrt(num)) + 1):
        if num % i == 0:
            factors.append(i)
            if i != num // i:
                factors.append(num // i)
    return sorted(factors)

In this optimized version:

  • We import the math module to use the sqrt() function for calculating the square root.
  • We iterate from 1 to the square root of num (inclusive).
  • If i is a factor, we append it to the factors list.
  • We also check if i is not equal to num // i (integer division). If they are not equal, it means there is a corresponding factor greater than the square root, so we append num // i to the factors list as well.
  • Finally, we return the sorted factors list.

Let’s test the optimized function with an example:

michael_birth_year = 1985
print(find_factors_optimized(michael_birth_year))

Output:

[1, 5, 397, 1985]

I have executed the above example code and added the screenshot below.

Find Factors of a Number in Python

The optimized function efficiently finds the factors of Michael’s birth year.

Check out How to Get Number of Months Between Two Dates in Python?

Conclusion

In this tutorial, I helped you to learn how to find factors of a number in Python. We saw the algorithm design and implemented solution, we also tried out the function and optimized the solution.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.