0
0
PythonProgramBeginner · 2 min read

Python Program to Find Factors of a Number

To find factors of a number in Python, use a loop with for i in range(1, n+1): and check if n % i == 0 to collect all factors.
📋

Examples

Input6
OutputFactors of 6 are: 1 2 3 6
Input13
OutputFactors of 13 are: 1 13
Input1
OutputFactors of 1 are: 1
🧠

How to Think About It

To find factors of a number, think about all numbers from 1 up to that number. For each, check if dividing the number by it leaves no remainder. If yes, that number is a factor.
📐

Algorithm

1
Get the input number n
2
Create an empty list to store factors
3
Loop i from 1 to n inclusive
4
Check if n modulo i equals 0
5
If yes, add i to the factors list
6
Return or print the list of factors
💻

Code

python
n = int(input('Enter a number: '))
factors = []
for i in range(1, n + 1):
    if n % i == 0:
        factors.append(i)
print('Factors of', n, 'are:', *factors)
Output
Enter a number: 6 Factors of 6 are: 1 2 3 6
🔍

Dry Run

Let's trace the number 6 through the code to find its factors.

1

Input

n = 6

2

Initialize factors list

factors = []

3

Loop i from 1 to 6

Check each i if divides 6 evenly

4

Check i=1

6 % 1 == 0, add 1 to factors

5

Check i=2

6 % 2 == 0, add 2 to factors

6

Check i=3

6 % 3 == 0, add 3 to factors

7

Check i=4

6 % 4 != 0, skip

8

Check i=5

6 % 5 != 0, skip

9

Check i=6

6 % 6 == 0, add 6 to factors

10

Print factors

Factors of 6 are: 1 2 3 6

in % i == 0?ActionFactors List
1TrueAdd 1[1]
2TrueAdd 2[1, 2]
3TrueAdd 3[1, 2, 3]
4FalseSkip[1, 2, 3]
5FalseSkip[1, 2, 3]
6TrueAdd 6[1, 2, 3, 6]
💡

Why This Works

Step 1: Loop through numbers

We check every number from 1 to n because factors must be within this range.

Step 2: Check divisibility

Using n % i == 0 tells us if i divides n without remainder, meaning i is a factor.

Step 3: Collect factors

We add all such i values to a list to show all factors at the end.

🔄

Alternative Approaches

Check up to square root
python
import math
n = int(input('Enter a number: '))
factors = []
for i in range(1, int(math.sqrt(n)) + 1):
    if n % i == 0:
        factors.append(i)
        if i != n // i:
            factors.append(n // i)
factors.sort()
print('Factors of', n, 'are:', *factors)
This method is faster for large numbers because it checks fewer values but needs sorting at the end.
Using list comprehension
python
n = int(input('Enter a number: '))
factors = [i for i in range(1, n + 1) if n % i == 0]
print('Factors of', n, 'are:', *factors)
This is a concise and readable way but still checks all numbers up to n.

Complexity: O(n) time, O(n) space

Time Complexity

The program checks every number from 1 to n, so it runs in linear time O(n).

Space Complexity

It stores all factors found in a list, which in the worst case can be up to O(n) if the number is 1.

Which Approach is Fastest?

Checking up to the square root reduces time to O(√n), making it faster for large numbers compared to checking all up to n.

ApproachTimeSpaceBest For
Check all up to nO(n)O(n)Small numbers, simplicity
Check up to sqrt(n)O(√n)O(n)Large numbers, better performance
List comprehensionO(n)O(n)Concise code, readability
💡
To speed up factor finding, check only up to the square root of the number and add both divisors.
⚠️
Beginners often forget to include the number itself as a factor or check beyond the number's range.