Find all the factors of a number in Python

Factorization is a fundamental concept in mathematics that involves expressing a number as a product of its prime factors. In Python, finding all the factors of a given number can be achieved through various methods. This Answer explores a simple yet effective approach using the provided Python code.

Calculating the factors of a number

To calculate the factors of a number, we need to find all the numbers that divide the given number evenly, leaving no remainder.

Note: Every number is divisible by 1 and itself.

Let’s calculate the factors of the number 12:

Stepwise calculation
Stepwise calculation

The final output will be as follows:

input = 12
output = 1, 2, 3, 4, 6, 12
Final output

Naive approach

The provided code defines a function named find_factors that takes two parameters: factors_list and number. The function aims to find all the factors of the given number and appends them to the factors_list.

def find_factors(factors_list, number):
i = 1
while i <= number:
if number % i != 0:
pass
else:
factors_list.append(i)
i += 1
return factors_list
factors_list = []
print(find_factors(factors_list, number = 320))

Let’s break down the code:

  • Lines 2–3: The find_factors function initializes a counter i to 1 and iterates through all numbers from 1 to the given number.

  • Lines 4–7: For each i, it checks if it’s a factor of the given number. For each divisor i, it checks if number % i equals zero, indicating that i is a factor. If true, i is appended to the factors_list.

  • Line 9: Finally, the function returns the list of factors.

The time complexity for the code above is O(n)O(n).

Optimized approach

In this approach, we iterate up to the square root of the given number to reduce the time complexity.

def find_factors(factors_list, number):
i = 1
while i * i <= number:
if number % i == 0:
factors_list.append(i)
if i != number // i:
factors_list.append(number // i)
i += 1
return factors_list
factors_list = []
print(find_factors(factors_list, number=320))

Let’s break down the code:

  • Line 3: The loop iterates up to the square root of the given number (i * i <= number).

  • Lines 4–7: The condition if number % i == 0 checks if i is a factor of number, and if true, both i and number // i are added to the factors_list. This eliminates the need to iterate up to the entire number, making the algorithm more efficient.

The overall time complexity of this algorithm is O(n)O(\sqrt{n}), making it more efficient compared to the original code, which had a time complexity of O(n)O(n).

Conclusion

The provided Python code offers a straightforward approach to finding all the factors of a given number. This is a valuable tool for various mathematical and programming applications where understanding the factors of a number is essential. As you continue your Python programming journey, exploring and understanding such fundamental concepts contributes to a solid foundation for more complex algorithms and mathematical operations.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved