How to Find Factors of a Given Number Using Python

The following python program prints all the positive factors of a given input number. This program demonstrates the use of functions, modulus operator, list data structure and if statement.

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

number = int(input("Please enter a number: "))
list_of_factors = get_all_factors(number)
print("factors of {} are: {}".format(number,list_of_factors))

Here is how the above program works,

  • We define a function get_all_factors() which takes a number as input and returns a list of factors.
  • This function initially creates an empty list. The function then iterates through number 1 to the input number using the built-in function range(). If the reminder is 0, we know that the number is a factor of input number and hence we add it to the factors list.
  • Finally the factors list is returned.
  • The program execution starts at the first line after the function declaration. We accept a user input using the built-in function input() and then convert to a number using the function int()
  • We then find all the factors of the number by calling the get_all_factors() function.
  • We use the built-in function print() with format() function to output the results.

Here is a sample output from the program,

Please enter a number: 25
factors of 25 are: [1, 5, 25]

We can easily extend the above program to print factors of all numbers below a given number,

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

number = int(input("Please enter the upper limit: "))

for i in range(1,number+1):
    list_of_factors = get_all_factors(i)
    print("factors of {} are: {}".format(i,list_of_factors))

Here is a sample output from the above program,

Please enter the upper limit: 6
factors of 1 are: [1]
factors of 2 are: [1, 2]
factors of 3 are: [1, 3]
factors of 4 are: [1, 2, 4]
factors of 5 are: [1, 5]
factors of 6 are: [1, 2, 3, 6]

We know that numbers with exact two factors are prime numbers! Hence we can extend the above program to indicate which of them are prime numbers,

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

number = int(input("Please enter the upper limit: "))

for i in range(1,number+1):
    list_of_factors = get_all_factors(i)
    if(len(list_of_factors) == 2):
        print("factors of {} are: {} and is prime!".format(i,list_of_factors))
    else:
        print("factors of {} are: {}".format(i,list_of_factors))

Sample output from the above program is,

Please enter the upper limit: 10
factors of 1 are: [1]
factors of 2 are: [1, 2] and is prime!
factors of 3 are: [1, 3] and is prime!
factors of 4 are: [1, 2, 4]
factors of 5 are: [1, 5] and is prime!
factors of 6 are: [1, 2, 3, 6]
factors of 7 are: [1, 7] and is prime!
factors of 8 are: [1, 2, 4, 8]
factors of 9 are: [1, 3, 9]
factors of 10 are: [1, 2, 5, 10]

Do you have a programming problem that you are unable to solve? Please send us your problem and we will publish the solution! Please email us.

Leave a Reply